add: new input scanning exercises

This commit is contained in:
Inanc Gumus
2019-05-07 12:17:07 +03:00
parent 6be081ef84
commit 556e99f186
17 changed files with 254 additions and 6 deletions

View File

@ -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() {

View 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
}

View File

@ -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 ravens 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.

View File

@ -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))
}

View File

@ -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 ravens 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.

View File

@ -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() {

View 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() {
}

View 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
}
}

View 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

View File

@ -0,0 +1,6 @@
learngoprogramming.com 10
learngoprogramming.com 10
golang.org
golang.org 6
blog.golang.org 20
blog.golang.org 10

View File

@ -0,0 +1,6 @@
learngoprogramming.com 10
learngoprogramming.com 10
golang.org -100
golang.org 6
blog.golang.org 20
blog.golang.org 10

View File

@ -0,0 +1,6 @@
learngoprogramming.com 10
learngoprogramming.com 10
golang.org FOUR
golang.org 6
blog.golang.org 20
blog.golang.org 10

View 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() {
}

View File

@ -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.
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.