From 556e99f186be6b87123d359278b596c805e7eef6 Mon Sep 17 00:00:00 2001 From: Inanc Gumus Date: Tue, 7 May 2019 12:17:07 +0300 Subject: [PATCH] add: new input scanning exercises --- .../exercises/02-unique-words/main.go | 9 +++- .../exercises/03-unique-words-2/main.go | 39 ++++++++++++++++++ .../03-unique-words-2/shakespeare.txt | 12 ++++++ .../03-unique-words-2/solution/main.go | 35 ++++++++++++++++ .../solution/shakespeare.txt | 12 ++++++ .../exercises/{03-grep => 04-grep}/main.go | 4 ++ .../{03-grep => 04-grep}/shakespeare.txt | 0 .../{03-grep => 04-grep}/solution/main.go | 0 .../solution/shakespeare.txt | 0 23-input-scanning/exercises/05-quit/main.go | 32 +++++++++++++++ .../exercises/05-quit/solution/main.go | 30 ++++++++++++++ .../exercises/06-log-parser/log.txt | 6 +++ .../06-log-parser/log_err_missing.txt | 6 +++ .../06-log-parser/log_err_negative.txt | 6 +++ .../exercises/06-log-parser/log_err_str.txt | 6 +++ .../exercises/06-log-parser/main.go | 41 +++++++++++++++++++ 23-input-scanning/exercises/README.md | 22 ++++++++-- 17 files changed, 254 insertions(+), 6 deletions(-) create mode 100644 23-input-scanning/exercises/03-unique-words-2/main.go create mode 100644 23-input-scanning/exercises/03-unique-words-2/shakespeare.txt create mode 100644 23-input-scanning/exercises/03-unique-words-2/solution/main.go create mode 100644 23-input-scanning/exercises/03-unique-words-2/solution/shakespeare.txt rename 23-input-scanning/exercises/{03-grep => 04-grep}/main.go (98%) rename 23-input-scanning/exercises/{03-grep => 04-grep}/shakespeare.txt (100%) rename 23-input-scanning/exercises/{03-grep => 04-grep}/solution/main.go (100%) rename 23-input-scanning/exercises/{03-grep => 04-grep}/solution/shakespeare.txt (100%) create mode 100644 23-input-scanning/exercises/05-quit/main.go create mode 100644 23-input-scanning/exercises/05-quit/solution/main.go create mode 100644 23-input-scanning/exercises/06-log-parser/log.txt create mode 100644 23-input-scanning/exercises/06-log-parser/log_err_missing.txt create mode 100644 23-input-scanning/exercises/06-log-parser/log_err_negative.txt create mode 100644 23-input-scanning/exercises/06-log-parser/log_err_str.txt create mode 100644 23-input-scanning/exercises/06-log-parser/main.go diff --git a/23-input-scanning/exercises/02-unique-words/main.go b/23-input-scanning/exercises/02-unique-words/main.go index 4974290..6efdf05 100644 --- a/23-input-scanning/exercises/02-unique-words/main.go +++ b/23-input-scanning/exercises/02-unique-words/main.go @@ -10,8 +10,8 @@ package main // --------------------------------------------------------- // EXERCISE: Unique Words // -// Create a program that counts the unique words from an -// input stream. +// Create a program that prints the total and unique words +// from an input stream. // // 1. Feed the shakespeare.txt to your program. // @@ -21,8 +21,13 @@ package main // // 4. Count the unique words using a map. // +// 5. Print the total and unique words. +// +// // EXPECTED OUTPUT +// // There are 99 words, 70 of them are unique. +// // --------------------------------------------------------- func main() { diff --git a/23-input-scanning/exercises/03-unique-words-2/main.go b/23-input-scanning/exercises/03-unique-words-2/main.go new file mode 100644 index 0000000..3a7f795 --- /dev/null +++ b/23-input-scanning/exercises/03-unique-words-2/main.go @@ -0,0 +1,39 @@ +// 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: Unique Words 2 +// +// Use your solution from the previous "Unique Words" +// exercise. +// +// Before adding the words to your map, remove the +// punctuation characters and numbers from them. +// +// +// BE CAREFUL +// +// Now the shakespeare.txt contains upper and lower +// case letters too. +// +// +// EXPECTED OUTPUT +// +// go run main.go < shakespeare.txt +// +// There are 100 words, 69 of them are unique. +// +// --------------------------------------------------------- + +func main() { + // This is the regular expression pattern you need to use: + // [^A-Za-z]+ + // + // Matches to any character but upper case and lower case letters +} diff --git a/23-input-scanning/exercises/03-unique-words-2/shakespeare.txt b/23-input-scanning/exercises/03-unique-words-2/shakespeare.txt new file mode 100644 index 0000000..dd43d8d --- /dev/null +++ b/23-input-scanning/exercises/03-unique-words-2/shakespeare.txt @@ -0,0 +1,12 @@ +Come, night. Come, Romeo. Come, thou day in night, +For thou wilt lie upon the wings of night +Whiter than new snow upon a raven’s back. +Come, gentle night, come, loving, black-browed night, +Give me my Romeo. And when I shall die, +Take him and cut him out in little stars, +And he will make the face of heaven so fine +That all the world will be in love with night +And pay no worship to the garish sun. +Oh, I have bought the mansion of a love, +But not possessed it, and though I am sold, +Not yet enjoyed. \ No newline at end of file diff --git a/23-input-scanning/exercises/03-unique-words-2/solution/main.go b/23-input-scanning/exercises/03-unique-words-2/solution/main.go new file mode 100644 index 0000000..9283705 --- /dev/null +++ b/23-input-scanning/exercises/03-unique-words-2/solution/main.go @@ -0,0 +1,35 @@ +package main + +// 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/ +// + +import ( + "bufio" + "fmt" + "os" + "regexp" + "strings" +) + +func main() { + in := bufio.NewScanner(os.Stdin) + in.Split(bufio.ScanWords) + + rx := regexp.MustCompile(`[^A-Za-z]+`) + + total, words := 0, make(map[string]int) + for in.Scan() { + total++ + + word := rx.ReplaceAllString(in.Text(), "") + word = strings.ToLower(word) + words[word]++ + } + + fmt.Printf("There are %d words, %d of them are unique.\n", + total, len(words)) +} diff --git a/23-input-scanning/exercises/03-unique-words-2/solution/shakespeare.txt b/23-input-scanning/exercises/03-unique-words-2/solution/shakespeare.txt new file mode 100644 index 0000000..dd43d8d --- /dev/null +++ b/23-input-scanning/exercises/03-unique-words-2/solution/shakespeare.txt @@ -0,0 +1,12 @@ +Come, night. Come, Romeo. Come, thou day in night, +For thou wilt lie upon the wings of night +Whiter than new snow upon a raven’s back. +Come, gentle night, come, loving, black-browed night, +Give me my Romeo. And when I shall die, +Take him and cut him out in little stars, +And he will make the face of heaven so fine +That all the world will be in love with night +And pay no worship to the garish sun. +Oh, I have bought the mansion of a love, +But not possessed it, and though I am sold, +Not yet enjoyed. \ No newline at end of file diff --git a/23-input-scanning/exercises/03-grep/main.go b/23-input-scanning/exercises/04-grep/main.go similarity index 98% rename from 23-input-scanning/exercises/03-grep/main.go rename to 23-input-scanning/exercises/04-grep/main.go index 4d8df59..a060d3d 100644 --- a/23-input-scanning/exercises/03-grep/main.go +++ b/23-input-scanning/exercises/04-grep/main.go @@ -15,10 +15,14 @@ package main // // 4. If no pattern is provided, print all the lines // +// // EXPECTED OUTPUT +// // go run main.go come < shakespeare.txt +// // come night come romeo come thou day in night // come gentle night come loving black-browed night +// // --------------------------------------------------------- func main() { diff --git a/23-input-scanning/exercises/03-grep/shakespeare.txt b/23-input-scanning/exercises/04-grep/shakespeare.txt similarity index 100% rename from 23-input-scanning/exercises/03-grep/shakespeare.txt rename to 23-input-scanning/exercises/04-grep/shakespeare.txt diff --git a/23-input-scanning/exercises/03-grep/solution/main.go b/23-input-scanning/exercises/04-grep/solution/main.go similarity index 100% rename from 23-input-scanning/exercises/03-grep/solution/main.go rename to 23-input-scanning/exercises/04-grep/solution/main.go diff --git a/23-input-scanning/exercises/03-grep/solution/shakespeare.txt b/23-input-scanning/exercises/04-grep/solution/shakespeare.txt similarity index 100% rename from 23-input-scanning/exercises/03-grep/solution/shakespeare.txt rename to 23-input-scanning/exercises/04-grep/solution/shakespeare.txt diff --git a/23-input-scanning/exercises/05-quit/main.go b/23-input-scanning/exercises/05-quit/main.go new file mode 100644 index 0000000..e830bf4 --- /dev/null +++ b/23-input-scanning/exercises/05-quit/main.go @@ -0,0 +1,32 @@ +package main + +// --------------------------------------------------------- +// EXERCISE: Quit +// +// Create a program that quits when a user types the +// same word twice. +// +// +// RESTRICTION +// +// The program should work case insensitive. +// +// +// EXPECTED OUTPUT +// +// go run main.go +// +// hey +// HEY +// TWICE! +// +// go run main.go +// +// hey +// hi +// HEY +// TWICE! +// --------------------------------------------------------- + +func main() { +} diff --git a/23-input-scanning/exercises/05-quit/solution/main.go b/23-input-scanning/exercises/05-quit/solution/main.go new file mode 100644 index 0000000..57ef469 --- /dev/null +++ b/23-input-scanning/exercises/05-quit/solution/main.go @@ -0,0 +1,30 @@ +// 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 ( + "bufio" + "fmt" + "os" + "strings" +) + +func main() { + in := bufio.NewScanner(os.Stdin) + + words := make(map[string]bool) + for in.Scan() { + w := strings.ToLower(in.Text()) + + if words[w] { + fmt.Println("TWICE!") + return + } + words[in.Text()] = true + } +} diff --git a/23-input-scanning/exercises/06-log-parser/log.txt b/23-input-scanning/exercises/06-log-parser/log.txt new file mode 100644 index 0000000..fb2432b --- /dev/null +++ b/23-input-scanning/exercises/06-log-parser/log.txt @@ -0,0 +1,6 @@ +learngoprogramming.com 10 +learngoprogramming.com 10 +golang.org 4 +golang.org 6 +blog.golang.org 20 +blog.golang.org 10 \ No newline at end of file diff --git a/23-input-scanning/exercises/06-log-parser/log_err_missing.txt b/23-input-scanning/exercises/06-log-parser/log_err_missing.txt new file mode 100644 index 0000000..fd8eff4 --- /dev/null +++ b/23-input-scanning/exercises/06-log-parser/log_err_missing.txt @@ -0,0 +1,6 @@ +learngoprogramming.com 10 +learngoprogramming.com 10 +golang.org +golang.org 6 +blog.golang.org 20 +blog.golang.org 10 \ No newline at end of file diff --git a/23-input-scanning/exercises/06-log-parser/log_err_negative.txt b/23-input-scanning/exercises/06-log-parser/log_err_negative.txt new file mode 100644 index 0000000..60485c0 --- /dev/null +++ b/23-input-scanning/exercises/06-log-parser/log_err_negative.txt @@ -0,0 +1,6 @@ +learngoprogramming.com 10 +learngoprogramming.com 10 +golang.org -100 +golang.org 6 +blog.golang.org 20 +blog.golang.org 10 \ No newline at end of file diff --git a/23-input-scanning/exercises/06-log-parser/log_err_str.txt b/23-input-scanning/exercises/06-log-parser/log_err_str.txt new file mode 100644 index 0000000..3a55bd7 --- /dev/null +++ b/23-input-scanning/exercises/06-log-parser/log_err_str.txt @@ -0,0 +1,6 @@ +learngoprogramming.com 10 +learngoprogramming.com 10 +golang.org FOUR +golang.org 6 +blog.golang.org 20 +blog.golang.org 10 \ No newline at end of file diff --git a/23-input-scanning/exercises/06-log-parser/main.go b/23-input-scanning/exercises/06-log-parser/main.go new file mode 100644 index 0000000..51540ab --- /dev/null +++ b/23-input-scanning/exercises/06-log-parser/main.go @@ -0,0 +1,41 @@ +package main + +// --------------------------------------------------------- +// EXERCISE: Log Parser from Stratch +// +// You've watched the lecture. Now, try to create the same +// log parser program on your own. Do not look at the lecture, +// and the existing source code. +// +// +// EXPECTED OUTPUT +// +// go run main.go < log.txt +// +// DOMAIN VISITS +// --------------------------------------------- +// blog.golang.org 30 +// golang.org 10 +// learngoprogramming.com 20 +// +// TOTAL 60 +// +// +// go run main.go < log_err_missing.txt +// +// wrong input: [golang.org] (line #3) +// +// +// go run main.go < log_err_negative.txt +// +// wrong input: "-100" (line #3) +// +// +// go run main.go < log_err_str.txt +// +// wrong input: "FOUR" (line #3) +// +// --------------------------------------------------------- + +func main() { +} diff --git a/23-input-scanning/exercises/README.md b/23-input-scanning/exercises/README.md index d63e6a2..01f48ac 100644 --- a/23-input-scanning/exercises/README.md +++ b/23-input-scanning/exercises/README.md @@ -2,14 +2,28 @@ Let's exercise with the scanner and maps. -1. **[Uppercaser](https://github.com/inancgumus/learngo/tree/master/23-project-log-parser/exercises/01-uppercaser)** +1. **[Uppercaser](https://github.com/inancgumus/learngo/tree/master/23-input-scanning/exercises/01-uppercaser)** Use a scanner to convert the lines to uppercase, and print them. -2. **[Unique Words](https://github.com/inancgumus/learngo/tree/master/23-project-log-parser/exercises/02-unique-words)** +2. **[Unique Words](https://github.com/inancgumus/learngo/tree/master/23-input-scanning/exercises/02-unique-words)** Create a program that counts the unique words from an input stream. -3. **[Grep Clone](https://github.com/inancgumus/learngo/tree/master/23-project-log-parser/exercises/03-grep)** +3. **[Unique Words 2](https://github.com/inancgumus/learngo/tree/master/23-input-scanning/exercises/03-unique-words-2)** - Create a grep clone. grep is a command-line utility for searching plain-text data for lines that match a specific pattern. \ No newline at end of file + Enhance the previous exercise: Before adding the words to your map, remove the punctuation characters and numbers from them. + +4. **[Grep Clone](https://github.com/inancgumus/learngo/tree/master/23-input-scanning/exercises/04-grep)** + + Create a grep clone. grep is a command-line utility for searching plain-text data for lines that match a specific pattern. + +5. **[Quit](https://github.com/inancgumus/learngo/tree/master/23-input-scanning/exercises/05-quit)** + + Create a program that quits when a user types the same word twice. + +6. **[Create the Log Parser program from scratch](https://github.com/inancgumus/learngo/tree/master/23-input-scanning/exercises/06-log-parser)** + + You've watched the lecture. Now, try to create the same log parser program on your own. Do not look at the lecture, and the existing source code. + + Click the link for more details. \ No newline at end of file