diff --git a/13-loops/02-project-lucky-number-game/02-game/main.go b/13-loops/02-project-lucky-number-game/02-game/main.go index 023f5ef..684468b 100644 --- a/13-loops/02-project-lucky-number-game/02-game/main.go +++ b/13-loops/02-project-lucky-number-game/02-game/main.go @@ -17,7 +17,7 @@ import ( const ( 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. Your mission is to guess one of those numbers. @@ -50,7 +50,7 @@ func main() { return } - for turn := 1; turn <= maxTurns; turn++ { + for turn := 0; turn < maxTurns; turn++ { n := rand.Intn(guess + 1) if n == guess { diff --git a/13-loops/exercises/02-lucky-number-exercises/01-first-turn-winner/solution/main.go b/13-loops/exercises/02-lucky-number-exercises/01-first-turn-winner/solution/main.go index 1e582ee..66a2307 100644 --- a/13-loops/exercises/02-lucky-number-exercises/01-first-turn-winner/solution/main.go +++ b/13-loops/exercises/02-lucky-number-exercises/01-first-turn-winner/solution/main.go @@ -17,7 +17,7 @@ import ( const ( 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. Your mission is to guess one of those numbers. diff --git a/13-loops/exercises/02-lucky-number-exercises/02-random-messages/solution/main.go b/13-loops/exercises/02-lucky-number-exercises/02-random-messages/solution/main.go index 1d5f6bc..1c7d4c0 100644 --- a/13-loops/exercises/02-lucky-number-exercises/02-random-messages/solution/main.go +++ b/13-loops/exercises/02-lucky-number-exercises/02-random-messages/solution/main.go @@ -17,7 +17,7 @@ import ( const ( 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. Your mission is to guess one of those numbers. @@ -49,7 +49,7 @@ func main() { return } - for turn := 1; turn <= maxTurns; turn++ { + for turn := 0; turn < maxTurns; turn++ { n := rand.Intn(guess + 1) if n == guess { diff --git a/13-loops/exercises/02-lucky-number-exercises/03-double-guesses/solution/main.go b/13-loops/exercises/02-lucky-number-exercises/03-double-guesses/solution/main.go index 6d1e4a5..7aee70a 100644 --- a/13-loops/exercises/02-lucky-number-exercises/03-double-guesses/solution/main.go +++ b/13-loops/exercises/02-lucky-number-exercises/03-double-guesses/solution/main.go @@ -17,7 +17,7 @@ import ( const ( 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. Your mission is to guess one of those numbers. @@ -63,7 +63,7 @@ func main() { min = guess2 } - for turn := 1; turn <= maxTurns; turn++ { + for turn := 0; turn < maxTurns; turn++ { n := rand.Intn(min + 1) if n == guess || n == guess2 { diff --git a/13-loops/exercises/02-lucky-number-exercises/04-verbose-mode/solution/main.go b/13-loops/exercises/02-lucky-number-exercises/04-verbose-mode/solution/main.go index 0931330..c3da230 100644 --- a/13-loops/exercises/02-lucky-number-exercises/04-verbose-mode/solution/main.go +++ b/13-loops/exercises/02-lucky-number-exercises/04-verbose-mode/solution/main.go @@ -17,7 +17,7 @@ import ( const ( 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. Your mission is to guess one of those numbers. diff --git a/13-loops/exercises/02-lucky-number-exercises/05-enough-picks/solution/main.go b/13-loops/exercises/02-lucky-number-exercises/05-enough-picks/solution/main.go index 047e3fb..f02c4be 100644 --- a/13-loops/exercises/02-lucky-number-exercises/05-enough-picks/solution/main.go +++ b/13-loops/exercises/02-lucky-number-exercises/05-enough-picks/solution/main.go @@ -17,7 +17,7 @@ import ( const ( 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. Your mission is to guess one of those numbers. @@ -54,9 +54,8 @@ func main() { min = guess } - for turn := 1; turn <= maxTurns; turn++ { + for turn := 0; turn < maxTurns; turn++ { n := rand.Intn(min + 1) - fmt.Println(n) if n == guess { fmt.Println("🎉 YOU WIN!") diff --git a/13-loops/exercises/02-lucky-number-exercises/06-dynamic-difficulty/solution/main.go b/13-loops/exercises/02-lucky-number-exercises/06-dynamic-difficulty/solution/main.go index 0838b18..75f233e 100644 --- a/13-loops/exercises/02-lucky-number-exercises/06-dynamic-difficulty/solution/main.go +++ b/13-loops/exercises/02-lucky-number-exercises/06-dynamic-difficulty/solution/main.go @@ -17,7 +17,7 @@ import ( const ( 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. Your mission is to guess one of those numbers. diff --git a/13-loops/exercises/03-word-finder-exercises/02-path-searcher/main.go b/13-loops/exercises/03-word-finder-exercises/02-path-searcher/main.go index 9ff4e1f..482a81d 100644 --- a/13-loops/exercises/03-word-finder-exercises/02-path-searcher/main.go +++ b/13-loops/exercises/03-word-finder-exercises/02-path-searcher/main.go @@ -14,23 +14,22 @@ package main // variable. // // Remove the corpus constant then get the corpus from the -// environment variable "Path" or "PATH" which -// constains paths to the executable programs on your -// operating system. +// environment variable "Path" or "PATH". // // HINTS -// 1. Search the web for what is an environment -// variable and how to use it, if you don't know -// what it is. +// 1. Search the web to find out what is an environment +// variable and how to use it (if you don't know +// 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. // // Search for it on the Go online documentation. // // 3. Look up for the necessary function for splitting -// the path variable into directories. It's in -// the "strings" package. +// the path variable into directory names. +// +// It's in the "path/filepath" package. // // EXAMPLE // For example, on my Mac, my PATH environment variable @@ -53,17 +52,30 @@ package main // the path environment variable when you run it on // a Windows or on a Mac (OS X) or on a Linux. // -// HINT -// 1. What you're looking for is the runtime.GOOS constant. -// 2. Get the operating system name using GOOS. -// 3. Adjust the path environment variable name and -// the directory separator accordingly. +// BONUS EXERCISE #2 +// Also find the directories for the directories that +// contains the searched query. // -// FOR EXAMPLE: On OS X, path environment variable's name -// is PATH and the separator is a colon `:`. +// And let it match in a case-insensitive manner. See the examples. // -// Or, on Windows, its name is Path and the separator is -// a semicolon `;`. +// EXAMPLE: +// 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() { diff --git a/13-loops/exercises/03-word-finder-exercises/02-path-searcher/solution/main.go b/13-loops/exercises/03-word-finder-exercises/02-path-searcher/solution/main.go index 00a34bf..f429397 100644 --- a/13-loops/exercises/03-word-finder-exercises/02-path-searcher/solution/main.go +++ b/13-loops/exercises/03-word-finder-exercises/02-path-searcher/solution/main.go @@ -10,26 +10,38 @@ package main import ( "fmt" "os" + "path/filepath" "strings" ) +const notFound = -1 + func main() { - // get and split the PATH environment variable - // this only works for the unix-based systems - words := strings.Split(os.Getenv("PATH"), ":") + // Get and split the PATH environment variable + + // 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:] -queries: + for _, q := range query { search: for i, w := range words { + q, w = strings.ToLower(q), strings.ToLower(w) + switch q { case "and", "or", "the": break search } - if q == w { + if strings.Index(w, q) != notFound { fmt.Printf("#%-2d: %q\n", i+1, w) - continue queries } } }