add: new input scanning exercises

This commit is contained in:
Inanc Gumus
2019-05-07 12:36:12 +03:00
parent 6be081ef84
commit 556e99f186
17 changed files with 254 additions and 6 deletions
@@ -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 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.
@@ -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 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.