update: lucky number game and its exercises
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user