Day 0: Hello, World.
Task
To complete this challenge, you must save a line of input from stdin to a variable, print Hello, World. on a single line, and finally print the value of your variable on a second line.
package main
import (
"fmt"
"bufio"
"os"
)
func main() {
reader := bufio.NewReader(os.Stdin)
inputString, _ := reader.ReadString('\n')
fmt.Println("Hello, World.")
fmt.Println(inputString)
}
Day 1: Data Type
Task
Declare 3 variables: one of type int, one of type double, and one of type String.
Read 3 lines of input from stdin (according to the sequence given in the Input Format section below) and initialize your 3 variables.
Use the
+
operator to perform the following operations:Print the sum of
i
plus your int variable on a new line.Print the sum of
d
plus your double variable to a scale of one decimal place on a new line.Concatenate
s
with the string you read as input and print the result on a new line.
package main
import (
"fmt"
"os"
"bufio"
"strconv"
)
func main() {
// Ignore this comment. You can still use the package "strconv".
var _ = strconv.Itoa
var i uint64 = 4
var d float64 = 4.0
var s string = "HackerRank "
scanner := bufio.NewScanner(os.Stdin)
// Declare second integer, double, and String variables.
var j uint64
var e float64
var t string
// Read and save an integer, double, and String to your variables.
scanner.Scan()
j,_ = strconv.ParseUint(scanner.Text(), 0, 64)
scanner.Scan()
e,_ = strconv.ParseFloat(scanner.Text(), 32)
scanner.Scan()
t = scanner.Text()
// Print the sum of both integer variables on a new line.
fmt.Println(i+j)
// Print the sum of the double variables on a new line.
fmt.Printf("%.1f\n",d+e)
// Concatenate and print the String variables on a new line
// The 's' variable above should be printed first.
fmt.Println(s+t)
}
Day 2: Operators
Task
Given the meal price (base cost of a meal), tip percent (the percentage of the meal price being added as tip), and tax percent (the percentage of the meal price being added as tax) for a meal, find and print the meal’s total cost.
package main
import "fmt"
var (
mealCost float64
tipPercent float64
taxPercent float64
)
func main() {
fmt.Scan(&mealCost)
fmt.Scan(&tipPercent)
fmt.Scan(&taxPercent)
tip := mealCost*tipPercent/100
tax := mealCost*taxPercent/100
totalCost := mealCost + tip + tax
fmt.Printf("The total meal cost is %v dollars.\n", int(totalCost+.5))
}
Day 3: Intro to Conditional Statements
Task
Given an integer,n
, perform the following conditional actions:
If n
is odd, print Weird
If n
is even and in the inclusive range of 2 to 5, print Not Weird
If n
is even and in the inclusive range of 6 to 20, print Weird
If n
is even and greater than 20, print Not Weird
package main
import "fmt"
var n int
func main() {
fmt.Scan(&n)
if n%2 != 0 {
fmt.Println("Weird")
}
if n%2 == 0 {
for i:=2; i<6; i++ {
if n == i {
fmt.Println("Not Weird")
}
}
}
if n%2 == 0 {
for i:=6; i<21; i++ {
if n == i {
fmt.Println("Weird")
}
}
}
if n%2 == 0 && n > 20 {
fmt.Println("Not Weird")
}
}
Day4: Class vs. Instance
Task
Write a Person class with an instance variable,age, and a constructor that takes an integer,initialAge
, as a parameter. The constructor must assign initailAge
to Age
after confirming the argument passed as initialAge
is not negative; if a negative argument is passed as initialAge
, the constructor should set age
to 0
and print Age is not valid, setting age to 0.
. In addition, you must write the following instance methods:
1.yearPasses() should increase the instance variable by .
2.amIOld() should perform the following conditional actions:
If
age
<13, print You are young..If
age
>= and , printYou are a teenager.
.Otherwise, print
You are old.
.
package main
import "fmt"
type person struct {
age int
}
func (p person) NewPerson(initialAge int) person {
//Add some more code to run some checks on initialAge
if initialAge < 0 {
p.age = 0
fmt.Println("Age is not valid, setting age to 0.")
}
p.age = initialAge
return p
}
func (p person) amIOld() {
//Do some computation in here and print out the correct statement to the console
if p.age < 13 {
fmt.Println("You are young.")
}
if p.age > 12 && p.age < 18 {
fmt.Println("You are a teenager.")
}
if p.age > 17 {
fmt.Println("You are old.")
}
}
func (p person) yearPasses() person {
//Increment the age of the person in here
p.age++
return p
}
func main() {
var T, age int
fmt.Scan(&T)
for i:=0; i<T; i++ {
fmt.Scan(&age)
p := person{age: age}
p = p.NewPerson(age)
p.amIOld()
for j:=0; j<3; j++{
p = p.yearPasses()
}
p.amIOld()
fmt.Println()
}
}