diff --git a/23-project-log-parser/exercises/01-uppercaser/main.go b/23-project-log-parser/exercises/01-uppercaser/main.go new file mode 100644 index 0000000..b887a45 --- /dev/null +++ b/23-project-log-parser/exercises/01-uppercaser/main.go @@ -0,0 +1,20 @@ +package main + +// --------------------------------------------------------- +// EXERCISE: Uppercaser +// +// Use a scanner to convert the lines to uppercase, and +// print them. +// +// 1. Feed the shakespeare.txt to your program. +// +// 2. Scan the input using a new Scanner. +// +// 3. Print each line. +// +// EXPECTED OUTPUT +// Please run the solution to see the expected output. +// --------------------------------------------------------- + +func main() { +} diff --git a/23-project-log-parser/exercises/01-uppercaser/shakespeare.txt b/23-project-log-parser/exercises/01-uppercaser/shakespeare.txt new file mode 100644 index 0000000..cd46cce --- /dev/null +++ b/23-project-log-parser/exercises/01-uppercaser/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 on a raven's back +come gentle night come loving black-browed night +give me my romeo and when he 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 love +but not possessed it and though i am sold +not yet enjoyed \ No newline at end of file diff --git a/23-project-log-parser/exercises/01-uppercaser/solution/main.go b/23-project-log-parser/exercises/01-uppercaser/solution/main.go new file mode 100644 index 0000000..6be3a82 --- /dev/null +++ b/23-project-log-parser/exercises/01-uppercaser/solution/main.go @@ -0,0 +1,23 @@ +// 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) + + for in.Scan() { + fmt.Println(strings.ToUpper(in.Text())) + } +} diff --git a/23-project-log-parser/exercises/01-uppercaser/solution/shakespeare.txt b/23-project-log-parser/exercises/01-uppercaser/solution/shakespeare.txt new file mode 100644 index 0000000..cd46cce --- /dev/null +++ b/23-project-log-parser/exercises/01-uppercaser/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 on a raven's back +come gentle night come loving black-browed night +give me my romeo and when he 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 love +but not possessed it and though i am sold +not yet enjoyed \ No newline at end of file diff --git a/23-project-log-parser/exercises/02-unique-words/main.go b/23-project-log-parser/exercises/02-unique-words/main.go new file mode 100644 index 0000000..1c368a8 --- /dev/null +++ b/23-project-log-parser/exercises/02-unique-words/main.go @@ -0,0 +1,37 @@ +// 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 +// +// Create a program that counts the unique words from an +// input stream. +// +// 1. Feed the shakespeare.txt to your program. +// +// 2. Scan the input using a new Scanner. +// +// 3. Configure the scanner to scan for the words. +// See the explanation inside the code below. +// +// 4. Count the unique words using a map. +// +// EXPECTED OUTPUT +// There are 99 words, 70 of them are unique. +// --------------------------------------------------------- + +func main() { + // Scanner can scan the lines, words, anything. + // Use the following code after creating the scanner to + // scan for the words instead. + // + // Below, I assumed that you put your scanner into the "in" variable. + + // in.Split(bufio.ScanWords) +} diff --git a/23-project-log-parser/exercises/02-unique-words/shakespeare.txt b/23-project-log-parser/exercises/02-unique-words/shakespeare.txt new file mode 100644 index 0000000..cd46cce --- /dev/null +++ b/23-project-log-parser/exercises/02-unique-words/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 on a raven's back +come gentle night come loving black-browed night +give me my romeo and when he 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 love +but not possessed it and though i am sold +not yet enjoyed \ No newline at end of file diff --git a/23-project-log-parser/exercises/02-unique-words/solution/main.go b/23-project-log-parser/exercises/02-unique-words/solution/main.go new file mode 100644 index 0000000..f551425 --- /dev/null +++ b/23-project-log-parser/exercises/02-unique-words/solution/main.go @@ -0,0 +1,27 @@ +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" +) + +func main() { + in := bufio.NewScanner(os.Stdin) + in.Split(bufio.ScanWords) + + total, words := 0, make(map[string]int) + for in.Scan() { + total++ + words[in.Text()]++ + } + fmt.Printf("There are %d words, %d of them are unique.\n", + total, len(words)) +} diff --git a/23-project-log-parser/exercises/02-unique-words/solution/shakespeare.txt b/23-project-log-parser/exercises/02-unique-words/solution/shakespeare.txt new file mode 100644 index 0000000..cd46cce --- /dev/null +++ b/23-project-log-parser/exercises/02-unique-words/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 on a raven's back +come gentle night come loving black-browed night +give me my romeo and when he 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 love +but not possessed it and though i am sold +not yet enjoyed \ No newline at end of file diff --git a/23-project-log-parser/exercises/03-grep/main.go b/23-project-log-parser/exercises/03-grep/main.go new file mode 100644 index 0000000..4d8df59 --- /dev/null +++ b/23-project-log-parser/exercises/03-grep/main.go @@ -0,0 +1,25 @@ +package main + +// --------------------------------------------------------- +// EXERCISE: Grep Clone +// +// Create a grep clone. grep is a command-line utility for +// searching plain-text data for lines that match a specific +// pattern. +// +// 1. Feed the shakespeare.txt to your program. +// +// 2. Accept a command-line argument for the pattern +// +// 3. Only print the lines that contains that pattern +// +// 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-project-log-parser/exercises/03-grep/shakespeare.txt b/23-project-log-parser/exercises/03-grep/shakespeare.txt new file mode 100644 index 0000000..cd46cce --- /dev/null +++ b/23-project-log-parser/exercises/03-grep/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 on a raven's back +come gentle night come loving black-browed night +give me my romeo and when he 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 love +but not possessed it and though i am sold +not yet enjoyed \ No newline at end of file diff --git a/23-project-log-parser/exercises/03-grep/solution/main.go b/23-project-log-parser/exercises/03-grep/solution/main.go new file mode 100644 index 0000000..878f6c5 --- /dev/null +++ b/23-project-log-parser/exercises/03-grep/solution/main.go @@ -0,0 +1,31 @@ +// 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) + + var pattern string + if args := os.Args[1:]; len(args) == 1 { + pattern = args[0] + } + + for in.Scan() { + s := in.Text() + if strings.Contains(s, pattern) { + fmt.Println(s) + } + } +} diff --git a/23-project-log-parser/exercises/03-grep/solution/shakespeare.txt b/23-project-log-parser/exercises/03-grep/solution/shakespeare.txt new file mode 100644 index 0000000..cd46cce --- /dev/null +++ b/23-project-log-parser/exercises/03-grep/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 on a raven's back +come gentle night come loving black-browed night +give me my romeo and when he 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 love +but not possessed it and though i am sold +not yet enjoyed \ No newline at end of file diff --git a/23-project-log-parser/exercises/README.md b/23-project-log-parser/exercises/README.md new file mode 100644 index 0000000..d63e6a2 --- /dev/null +++ b/23-project-log-parser/exercises/README.md @@ -0,0 +1,15 @@ +# Exercises + +Let's exercise with the scanner and maps. + +1. **[Uppercaser](https://github.com/inancgumus/learngo/tree/master/23-project-log-parser/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)** + + 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)** + + 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