update: lucky number game and its exercises
This commit is contained in:
@ -17,7 +17,7 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
maxTurns = 5 // less is more difficult
|
maxTurns = 5 // less is more difficult
|
||||||
usage = `Welcome to the Lucky Number Game!
|
usage = `Welcome to the Lucky Number Game! 🍀
|
||||||
|
|
||||||
The program will pick %d random numbers.
|
The program will pick %d random numbers.
|
||||||
Your mission is to guess one of those numbers.
|
Your mission is to guess one of those numbers.
|
||||||
@ -50,7 +50,7 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for turn := 1; turn <= maxTurns; turn++ {
|
for turn := 0; turn < maxTurns; turn++ {
|
||||||
n := rand.Intn(guess + 1)
|
n := rand.Intn(guess + 1)
|
||||||
|
|
||||||
if n == guess {
|
if n == guess {
|
||||||
|
@ -17,7 +17,7 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
maxTurns = 5 // less is more difficult
|
maxTurns = 5 // less is more difficult
|
||||||
usage = `Welcome to the Lucky Number Game!
|
usage = `Welcome to the Lucky Number Game! 🍀
|
||||||
|
|
||||||
The program will pick %d random numbers.
|
The program will pick %d random numbers.
|
||||||
Your mission is to guess one of those numbers.
|
Your mission is to guess one of those numbers.
|
||||||
|
@ -17,7 +17,7 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
maxTurns = 5 // less is more difficult
|
maxTurns = 5 // less is more difficult
|
||||||
usage = `Welcome to the Lucky Number Game!
|
usage = `Welcome to the Lucky Number Game! 🍀
|
||||||
|
|
||||||
The program will pick %d random numbers.
|
The program will pick %d random numbers.
|
||||||
Your mission is to guess one of those numbers.
|
Your mission is to guess one of those numbers.
|
||||||
@ -49,7 +49,7 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for turn := 1; turn <= maxTurns; turn++ {
|
for turn := 0; turn < maxTurns; turn++ {
|
||||||
n := rand.Intn(guess + 1)
|
n := rand.Intn(guess + 1)
|
||||||
|
|
||||||
if n == guess {
|
if n == guess {
|
||||||
|
@ -17,7 +17,7 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
maxTurns = 5 // less is more difficult
|
maxTurns = 5 // less is more difficult
|
||||||
usage = `Welcome to the Lucky Number Game!
|
usage = `Welcome to the Lucky Number Game! 🍀
|
||||||
|
|
||||||
The program will pick %d random numbers.
|
The program will pick %d random numbers.
|
||||||
Your mission is to guess one of those numbers.
|
Your mission is to guess one of those numbers.
|
||||||
@ -63,7 +63,7 @@ func main() {
|
|||||||
min = guess2
|
min = guess2
|
||||||
}
|
}
|
||||||
|
|
||||||
for turn := 1; turn <= maxTurns; turn++ {
|
for turn := 0; turn < maxTurns; turn++ {
|
||||||
n := rand.Intn(min + 1)
|
n := rand.Intn(min + 1)
|
||||||
|
|
||||||
if n == guess || n == guess2 {
|
if n == guess || n == guess2 {
|
||||||
|
@ -17,7 +17,7 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
maxTurns = 5 // less is more difficult
|
maxTurns = 5 // less is more difficult
|
||||||
usage = `Welcome to the Lucky Number Game!
|
usage = `Welcome to the Lucky Number Game! 🍀
|
||||||
|
|
||||||
The program will pick %d random numbers.
|
The program will pick %d random numbers.
|
||||||
Your mission is to guess one of those numbers.
|
Your mission is to guess one of those numbers.
|
||||||
|
@ -17,7 +17,7 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
maxTurns = 5 // less is more difficult
|
maxTurns = 5 // less is more difficult
|
||||||
usage = `Welcome to the Lucky Number Game!
|
usage = `Welcome to the Lucky Number Game! 🍀
|
||||||
|
|
||||||
The program will pick %d random numbers.
|
The program will pick %d random numbers.
|
||||||
Your mission is to guess one of those numbers.
|
Your mission is to guess one of those numbers.
|
||||||
@ -54,9 +54,8 @@ func main() {
|
|||||||
min = guess
|
min = guess
|
||||||
}
|
}
|
||||||
|
|
||||||
for turn := 1; turn <= maxTurns; turn++ {
|
for turn := 0; turn < maxTurns; turn++ {
|
||||||
n := rand.Intn(min + 1)
|
n := rand.Intn(min + 1)
|
||||||
fmt.Println(n)
|
|
||||||
|
|
||||||
if n == guess {
|
if n == guess {
|
||||||
fmt.Println("🎉 YOU WIN!")
|
fmt.Println("🎉 YOU WIN!")
|
||||||
|
@ -17,7 +17,7 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
maxTurns = 5 // less is more difficult
|
maxTurns = 5 // less is more difficult
|
||||||
usage = `Welcome to the Lucky Number Game!
|
usage = `Welcome to the Lucky Number Game! 🍀
|
||||||
|
|
||||||
The program will pick %d random numbers.
|
The program will pick %d random numbers.
|
||||||
Your mission is to guess one of those numbers.
|
Your mission is to guess one of those numbers.
|
||||||
|
@ -14,23 +14,22 @@ package main
|
|||||||
// variable.
|
// variable.
|
||||||
//
|
//
|
||||||
// Remove the corpus constant then get the corpus from the
|
// Remove the corpus constant then get the corpus from the
|
||||||
// environment variable "Path" or "PATH" which
|
// environment variable "Path" or "PATH".
|
||||||
// constains paths to the executable programs on your
|
|
||||||
// operating system.
|
|
||||||
//
|
//
|
||||||
// HINTS
|
// HINTS
|
||||||
// 1. Search the web for what is an environment
|
// 1. Search the web to find out what is an environment
|
||||||
// variable and how to use it, if you don't know
|
// variable and how to use it (if you don't know
|
||||||
// what it is.
|
// what it is already).
|
||||||
//
|
//
|
||||||
// 2. Look up for the necessary function for getting
|
// 2. Look up for the necessary functions for getting
|
||||||
// an environment variable. It's in the "os" package.
|
// an environment variable. It's in the "os" package.
|
||||||
//
|
//
|
||||||
// Search for it on the Go online documentation.
|
// Search for it on the Go online documentation.
|
||||||
//
|
//
|
||||||
// 3. Look up for the necessary function for splitting
|
// 3. Look up for the necessary function for splitting
|
||||||
// the path variable into directories. It's in
|
// the path variable into directory names.
|
||||||
// the "strings" package.
|
//
|
||||||
|
// It's in the "path/filepath" package.
|
||||||
//
|
//
|
||||||
// EXAMPLE
|
// EXAMPLE
|
||||||
// For example, on my Mac, my PATH environment variable
|
// For example, on my Mac, my PATH environment variable
|
||||||
@ -53,17 +52,30 @@ package main
|
|||||||
// the path environment variable when you run it on
|
// the path environment variable when you run it on
|
||||||
// a Windows or on a Mac (OS X) or on a Linux.
|
// a Windows or on a Mac (OS X) or on a Linux.
|
||||||
//
|
//
|
||||||
// HINT
|
// BONUS EXERCISE #2
|
||||||
// 1. What you're looking for is the runtime.GOOS constant.
|
// Also find the directories for the directories that
|
||||||
// 2. Get the operating system name using GOOS.
|
// contains the searched query.
|
||||||
// 3. Adjust the path environment variable name and
|
|
||||||
// the directory separator accordingly.
|
|
||||||
//
|
//
|
||||||
// FOR EXAMPLE: On OS X, path environment variable's name
|
// And let it match in a case-insensitive manner. See the examples.
|
||||||
// is PATH and the separator is a colon `:`.
|
|
||||||
//
|
//
|
||||||
// Or, on Windows, its name is Path and the separator is
|
// EXAMPLE:
|
||||||
// a semicolon `;`.
|
// Let's say:
|
||||||
|
// PATH="/usr/local/bin:/sbin:/Users/inanc/go/bin"
|
||||||
|
//
|
||||||
|
// So, if the user runs the program like this:
|
||||||
|
// go run main.go bin
|
||||||
|
//
|
||||||
|
// It should print this:
|
||||||
|
// #1 : "/usr/local/bin"
|
||||||
|
// #2 : "/sbin"
|
||||||
|
// #3 : "/Users/inanc/go/bin"
|
||||||
|
//
|
||||||
|
// Or like this (case insensitive):
|
||||||
|
// go run main.go Us
|
||||||
|
//
|
||||||
|
// Output:
|
||||||
|
// #1 : "/usr/local/bin"
|
||||||
|
// #2 : "/Users/inanc/go/bin"
|
||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -10,26 +10,38 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const notFound = -1
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// get and split the PATH environment variable
|
// Get and split the PATH environment variable
|
||||||
// this only works for the unix-based systems
|
|
||||||
words := strings.Split(os.Getenv("PATH"), ":")
|
// SplitList function automatically finds the
|
||||||
|
// separator for the path env variable
|
||||||
|
words := filepath.SplitList(os.Getenv("PATH"))
|
||||||
|
|
||||||
|
// Alternative way, but above one is better:
|
||||||
|
// words := strings.Split(
|
||||||
|
// os.Getenv("PATH"),
|
||||||
|
// string(os.PathListSeparator))
|
||||||
|
|
||||||
query := os.Args[1:]
|
query := os.Args[1:]
|
||||||
queries:
|
|
||||||
for _, q := range query {
|
for _, q := range query {
|
||||||
search:
|
search:
|
||||||
for i, w := range words {
|
for i, w := range words {
|
||||||
|
q, w = strings.ToLower(q), strings.ToLower(w)
|
||||||
|
|
||||||
switch q {
|
switch q {
|
||||||
case "and", "or", "the":
|
case "and", "or", "the":
|
||||||
break search
|
break search
|
||||||
}
|
}
|
||||||
|
|
||||||
if q == w {
|
if strings.Index(w, q) != notFound {
|
||||||
fmt.Printf("#%-2d: %q\n", i+1, w)
|
fmt.Printf("#%-2d: %q\n", i+1, w)
|
||||||
continue queries
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user