Skip to content

Instantly share code, notes, and snippets.

@bfitzsimmons
Last active September 25, 2017 20:27
Show Gist options
  • Select an option

  • Save bfitzsimmons/7dd2c7b5cb19f8632272 to your computer and use it in GitHub Desktop.

Select an option

Save bfitzsimmons/7dd2c7b5cb19f8632272 to your computer and use it in GitHub Desktop.
Hackerrank -- 30 Days of Code -- Day 4 challenge solution.
package main
import "fmt"
// Person is the main object we're working with.
type Person struct {
Age int
}
// Initialize sets things up.
func (p *Person) Initialize(initialAge int) {
if initialAge < 0 {
p.Age = 0
fmt.Println("This person is not valid, setting age to 0.")
} else {
p.Age = initialAge
}
}
// AmIOld checks to see if the person is old.
func (p *Person) AmIOld() {
switch {
case p.Age < 13:
fmt.Println("You are young.")
case p.Age >= 13 && p.Age < 18:
fmt.Println("You are a teenager.")
default:
fmt.Println("You are old.")
}
}
// YearPasses ages the person by one year.
func (p *Person) YearPasses() {
p.Age++
}
func main() {
var num, age int
// Get the number of ages being submitted.
fmt.Scan(&num)
// Get the age from the input.
for i := 0; i < num; i++ {
fmt.Scan(&age)
// Create our person; check their age; age them 3 years; check their age again.
p := Person{}
p.Initialize(age)
p.AmIOld()
for j := 0; j < 3; j++ {
p.YearPasses()
}
p.AmIOld()
fmt.Println()
}
}
package main
import "fmt"
// Person is the main object we're working with.
type Person struct {
Age int
}
// Initialize sets things up.
func (p *Person) Initialize(initialAge int) {
}
// AmIOld checks to see if the person is old.
func (p *Person) AmIOld() {
}
// YearPasses ages the person by one year.
func (p *Person) YearPasses() {
}
func main() {
var num, age int
// Get the number of ages being submitted.
fmt.Scan(&num)
// Get the age from the input.
for i := 0; i < num; i++ {
fmt.Scan(&age)
// Create our person; check their age; age them 3 years; check their age again.
p := Person{}
p.Initialize(age)
p.AmIOld()
for j := 0; j < 3; j++ {
p.YearPasses()
}
p.AmIOld()
fmt.Println()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment