Last active
August 29, 2015 13:57
-
-
Save makyo/9493927 to your computer and use it in GitHub Desktop.
Coding question
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
import "strings" | |
func main() { | |
fmt.Println("Think of a number from 1 to 100...") | |
currGuess := 50 | |
for { | |
var response string | |
fmt.Printf("I guess %d - am I too [h]igh, too [l]ow, or [r]ight on? ", currGuess) | |
fmt.Scanln(&response) | |
if strings.HasPrefix(response, "h") { | |
currGuess-- | |
} else if strings.HasPrefix(response, "l") { | |
currGuess++ | |
} else if strings.HasPrefix(response, "r") { | |
break | |
} | |
} | |
fmt.Printf("Got it! Your number is %v\n", currGuess) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
import "strings" | |
func main() { | |
fmt.Println("Think of a number from 1 to 100...") | |
currGuess := 50 | |
max := 100 | |
min := 0 | |
for { | |
var response string | |
fmt.Printf("I guess %d - am I too [h]igh, too [l]ow, or [r]ight on? ", currGuess) | |
fmt.Scanln(&response) | |
if strings.HasPrefix(response, "h") { | |
max = currGuess | |
} else if strings.HasPrefix(response, "l") { | |
min = currGuess | |
} else if strings.HasPrefix(response, "r") { | |
break | |
} | |
currGuess = min + (max-min)/2 | |
} | |
fmt.Printf("Got it! Your number is %v\n", currGuess) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment