開発環境
- macOS Catalina - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Go (プログラミング言語)
Go Systems Programming: Master Linux and Unix system level programming with Go (Mihalis Tsoukalos(著)、Packt Publishing)のChapter 3(Advanced Go Features)、Exercises 12.の解答を求めてみる。
コード
package main
import (
"fmt"
"os"
"strconv"
"strings"
)
func main() {
threshold := 0
if len(os.Args) > 1 {
if n, err := strconv.Atoi(os.Args[1]); err != nil {
fmt.Println("Usage: occurences [threshold]")
} else {
threshold = n
}
}
strs := []string{
"1 b 3 1 a a b",
"11 a 1 1 1 1 a a",
"-1 b 1 -4 a 1",
}
counts := map[string]int{}
for _, s := range strs {
for _, word := range strings.Fields(s) {
counts[word]++
}
}
for k, v := range counts {
if v > threshold {
fmt.Printf("%v -> %v\n", k, v)
}
}
}
入出力結果(Zsh、PowerShell、Terminal)
% go build
% ./occurrences
1 -> 8
b -> 3
3 -> 1
a -> 6
11 -> 1
-1 -> 1
-4 -> 1
% ./occurrences 0
11 -> 1
-1 -> 1
-4 -> 1
1 -> 8
b -> 3
3 -> 1
a -> 6
% ./occurrences 1
1 -> 8
b -> 3
a -> 6
% ./occurrences 2
1 -> 8
b -> 3
a -> 6
% ./occurrences 3
1 -> 8
a -> 6
% ./occurrences 4
1 -> 8
a -> 6
% ./occurrences 5
1 -> 8
a -> 6
% ./occurrences 6
1 -> 8
% ./occurrences 7
1 -> 8
% ./occurrences 8
% ./occurrences a
Usage: occurrences [threshold]
1 -> 8
b -> 3
3 -> 1
a -> 6
11 -> 1
-1 -> 1
-4 -> 1
%
0 コメント:
コメントを投稿