update: lucky number game and its exercises

This commit is contained in:
Inanc Gumus
2018-10-19 11:52:52 +03:00
parent cde4e6632c
commit e4f3be01e4
9 changed files with 59 additions and 36 deletions

View File

@@ -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
}
}
}