add: array exercises
This commit is contained in:
56
14-arrays/exercises/10-hipsters-love-search/main.go
Normal file
56
14-arrays/exercises/10-hipsters-love-search/main.go
Normal file
@ -0,0 +1,56 @@
|
||||
// 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: Hipster's Love Search Engine
|
||||
//
|
||||
// Your goal is to let people search for books of Hipster's Love Bookstore.
|
||||
//
|
||||
// 1. Create an array with these book titles:
|
||||
// Kafka's Revenge
|
||||
// Stay Golden
|
||||
// Everythingship
|
||||
// Kafka's Revenge 2nd Edition
|
||||
//
|
||||
// 2. Get the search query from the command-line argument
|
||||
//
|
||||
// 3. Search for the books in the books array
|
||||
//
|
||||
// 4. When the programs find the book, print it.
|
||||
// 5. Otherwise, print that the book doesn't exist.
|
||||
//
|
||||
// 6. Handle the errors.
|
||||
//
|
||||
// RESTRICTION:
|
||||
// + The search should be case insensitive.
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// go run main.go
|
||||
// Tell me a book title
|
||||
//
|
||||
// go run main.go STAY
|
||||
// Search Results:
|
||||
// + Stay Golden
|
||||
//
|
||||
// go run main.go sTaY
|
||||
// Search Results:
|
||||
// + Stay Golden
|
||||
//
|
||||
// go run main.go "Kafka's Revenge"
|
||||
// Search Results:
|
||||
// + Kafka's Revenge
|
||||
// + Kafka's Revenge 2nd Edition
|
||||
//
|
||||
// go run main.go void
|
||||
// Search Results:
|
||||
// We don't have the book: "void"
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
}
|
44
14-arrays/exercises/10-hipsters-love-search/solution/main.go
Normal file
44
14-arrays/exercises/10-hipsters-love-search/solution/main.go
Normal file
@ -0,0 +1,44 @@
|
||||
// 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"
|
||||
)
|
||||
|
||||
func main() {
|
||||
books := [4]string{
|
||||
"Kafka's Revenge",
|
||||
"Stay Golden",
|
||||
"Everythingship",
|
||||
"Kafka's Revenge 2nd Edition",
|
||||
}
|
||||
|
||||
args := os.Args[1:]
|
||||
if len(args) != 1 {
|
||||
fmt.Println("Tell me a book title")
|
||||
return
|
||||
}
|
||||
query := strings.ToLower(args[0])
|
||||
|
||||
fmt.Println("Search Results:")
|
||||
|
||||
var found bool
|
||||
for _, v := range books {
|
||||
if strings.Contains(strings.ToLower(v), query) {
|
||||
fmt.Println("+", v)
|
||||
found = true
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
fmt.Printf("We don't have the book: %q\n", query)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user