Files
learngo/23-input-scanning/exercises/03-unique-words-2/solution/main.go

37 lines
742 B
Go
Raw Normal View History

2019-04-12 16:55:26 +03:00
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
2019-10-30 19:34:44 +03:00
// For more tutorials : https://learngoprogramming.com
// In-person training : https://www.linkedin.com/in/inancgumus/
// Follow me on twitter: https://twitter.com/inancgumus
package main
2019-04-12 16:55:26 +03:00
import (
"bufio"
"fmt"
"os"
2019-05-07 12:17:07 +03:00
"regexp"
2019-04-12 16:55:26 +03:00
"strings"
)
func main() {
in := bufio.NewScanner(os.Stdin)
2019-05-07 12:17:07 +03:00
in.Split(bufio.ScanWords)
2019-04-12 16:55:26 +03:00
2019-05-07 12:17:07 +03:00
rx := regexp.MustCompile(`[^A-Za-z]+`)
2019-04-12 16:55:26 +03:00
2019-05-07 12:17:07 +03:00
total, words := 0, make(map[string]int)
2019-04-12 16:55:26 +03:00
for in.Scan() {
2019-05-07 12:17:07 +03:00
total++
word := rx.ReplaceAllString(in.Text(), "")
word = strings.ToLower(word)
words[word]++
2019-04-12 16:55:26 +03:00
}
2019-05-07 12:17:07 +03:00
fmt.Printf("There are %d words, %d of them are unique.\n",
total, len(words))
2019-04-12 16:55:26 +03:00
}