update: log parser dir to input scanning
This commit is contained in:
29
23-input-scanning/exercises/02-unique-words/main.go
Normal file
29
23-input-scanning/exercises/02-unique-words/main.go
Normal file
@@ -0,0 +1,29 @@
|
||||
// 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.
|
||||
//
|
||||
// 4. Count the unique words using a map.
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// There are 99 words, 70 of them are unique.
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
}
|
12
23-input-scanning/exercises/02-unique-words/shakespeare.txt
Normal file
12
23-input-scanning/exercises/02-unique-words/shakespeare.txt
Normal 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 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
|
27
23-input-scanning/exercises/02-unique-words/solution/main.go
Normal file
27
23-input-scanning/exercises/02-unique-words/solution/main.go
Normal file
@@ -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))
|
||||
}
|
@@ -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
|
Reference in New Issue
Block a user