add: loop for statement exercises
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE: Case Insensitive Search
|
||||
//
|
||||
// Allow for case-insensitive searching
|
||||
//
|
||||
// EXAMPLE
|
||||
// Let's say that the user runs the program like this:
|
||||
// go run main.go LAZY
|
||||
//
|
||||
// Or like this: go run main.go lAzY
|
||||
// Or like this: go run main.go lazy
|
||||
//
|
||||
// For all cases above, the program should find
|
||||
// the "lazy" keyword.
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
}
|
@@ -0,0 +1,40 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const corpus = "lazy cat jumps again and again and again"
|
||||
|
||||
func main() {
|
||||
words := strings.Fields(corpus)
|
||||
query := os.Args[1:]
|
||||
|
||||
queries:
|
||||
for _, q := range query {
|
||||
// case insensitive search
|
||||
q = strings.ToLower(q)
|
||||
|
||||
search:
|
||||
for i, w := range words {
|
||||
switch q {
|
||||
case "and", "or", "the":
|
||||
break search
|
||||
}
|
||||
|
||||
if q == w {
|
||||
fmt.Printf("#%-2d: %q\n", i+1, w)
|
||||
continue queries
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,82 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE: Path Searcher
|
||||
//
|
||||
// Your program should search inside the path environment
|
||||
// variable.
|
||||
//
|
||||
// Remove the corpus constant then get the corpus from the
|
||||
// environment variable "Path" or "PATH".
|
||||
//
|
||||
// HINTS
|
||||
// 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 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 directory names.
|
||||
//
|
||||
// It's in the "path/filepath" package.
|
||||
//
|
||||
// EXAMPLE
|
||||
// For example, on my Mac, my PATH environment variable
|
||||
// looks like this:
|
||||
//
|
||||
// "/usr/local/bin:/sbin:/Users/inanc/go/bin"
|
||||
//
|
||||
// So, if the user runs the program like this:
|
||||
//
|
||||
// go run main.go /sbin
|
||||
//
|
||||
// It should print this:
|
||||
//
|
||||
// #2 : "/sbin"
|
||||
// ---------------------------------------------------------
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// BONUS EXERCISE
|
||||
// Make your program cross platform. So, it can search
|
||||
// the path environment variable when you run it on
|
||||
// a Windows or on a Mac (OS X) or on a Linux.
|
||||
//
|
||||
// BONUS EXERCISE #2
|
||||
// Also find the directories for the directories that
|
||||
// contains the searched query.
|
||||
//
|
||||
// And let it match in a case-insensitive manner. See the examples.
|
||||
//
|
||||
// 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() {
|
||||
}
|
@@ -0,0 +1,48 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const notFound = -1
|
||||
|
||||
func main() {
|
||||
// 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:]
|
||||
|
||||
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 strings.Index(w, q) != notFound {
|
||||
fmt.Printf("#%-2d: %q\n", i+1, w)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user