add: new input scanning exercises
This commit is contained in:
@ -10,8 +10,8 @@ package main
|
|||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
// EXERCISE: Unique Words
|
// EXERCISE: Unique Words
|
||||||
//
|
//
|
||||||
// Create a program that counts the unique words from an
|
// Create a program that prints the total and unique words
|
||||||
// input stream.
|
// from an input stream.
|
||||||
//
|
//
|
||||||
// 1. Feed the shakespeare.txt to your program.
|
// 1. Feed the shakespeare.txt to your program.
|
||||||
//
|
//
|
||||||
@ -21,8 +21,13 @@ package main
|
|||||||
//
|
//
|
||||||
// 4. Count the unique words using a map.
|
// 4. Count the unique words using a map.
|
||||||
//
|
//
|
||||||
|
// 5. Print the total and unique words.
|
||||||
|
//
|
||||||
|
//
|
||||||
// EXPECTED OUTPUT
|
// EXPECTED OUTPUT
|
||||||
|
//
|
||||||
// There are 99 words, 70 of them are unique.
|
// There are 99 words, 70 of them are unique.
|
||||||
|
//
|
||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
39
23-input-scanning/exercises/03-unique-words-2/main.go
Normal file
39
23-input-scanning/exercises/03-unique-words-2/main.go
Normal file
@ -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
|
||||||
|
}
|
@ -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.
|
@ -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))
|
||||||
|
}
|
@ -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.
|
@ -15,10 +15,14 @@ package main
|
|||||||
//
|
//
|
||||||
// 4. If no pattern is provided, print all the lines
|
// 4. If no pattern is provided, print all the lines
|
||||||
//
|
//
|
||||||
|
//
|
||||||
// EXPECTED OUTPUT
|
// EXPECTED OUTPUT
|
||||||
|
//
|
||||||
// go run main.go come < shakespeare.txt
|
// go run main.go come < shakespeare.txt
|
||||||
|
//
|
||||||
// come night come romeo come thou day in night
|
// come night come romeo come thou day in night
|
||||||
// come gentle night come loving black-browed night
|
// come gentle night come loving black-browed night
|
||||||
|
//
|
||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
|
|
||||||
func main() {
|
func main() {
|
32
23-input-scanning/exercises/05-quit/main.go
Normal file
32
23-input-scanning/exercises/05-quit/main.go
Normal file
@ -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() {
|
||||||
|
}
|
30
23-input-scanning/exercises/05-quit/solution/main.go
Normal file
30
23-input-scanning/exercises/05-quit/solution/main.go
Normal file
@ -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
|
||||||
|
}
|
||||||
|
}
|
6
23-input-scanning/exercises/06-log-parser/log.txt
Normal file
6
23-input-scanning/exercises/06-log-parser/log.txt
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
learngoprogramming.com 10
|
||||||
|
learngoprogramming.com 10
|
||||||
|
golang.org 4
|
||||||
|
golang.org 6
|
||||||
|
blog.golang.org 20
|
||||||
|
blog.golang.org 10
|
@ -0,0 +1,6 @@
|
|||||||
|
learngoprogramming.com 10
|
||||||
|
learngoprogramming.com 10
|
||||||
|
golang.org
|
||||||
|
golang.org 6
|
||||||
|
blog.golang.org 20
|
||||||
|
blog.golang.org 10
|
@ -0,0 +1,6 @@
|
|||||||
|
learngoprogramming.com 10
|
||||||
|
learngoprogramming.com 10
|
||||||
|
golang.org -100
|
||||||
|
golang.org 6
|
||||||
|
blog.golang.org 20
|
||||||
|
blog.golang.org 10
|
@ -0,0 +1,6 @@
|
|||||||
|
learngoprogramming.com 10
|
||||||
|
learngoprogramming.com 10
|
||||||
|
golang.org FOUR
|
||||||
|
golang.org 6
|
||||||
|
blog.golang.org 20
|
||||||
|
blog.golang.org 10
|
41
23-input-scanning/exercises/06-log-parser/main.go
Normal file
41
23-input-scanning/exercises/06-log-parser/main.go
Normal file
@ -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() {
|
||||||
|
}
|
@ -2,14 +2,28 @@
|
|||||||
|
|
||||||
Let's exercise with the scanner and maps.
|
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.
|
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.
|
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)**
|
||||||
|
|
||||||
|
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.
|
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.
|
Reference in New Issue
Block a user