開発環境
- macOS Catalina - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Go (プログラミング言語)
入門Goプログラミング (Nathan Youngman(著)、Roger Peppé(著)、吉川 邦夫(監修, 翻訳)、翔泳社)のUNIT 4(コレクション)、LESSON 19(守備範囲が広いマップ)の練習問題の解答を求めてみる。
コード
package main
import (
"fmt"
"strings"
)
func main() {
s := "As far as eye could reach he saw nothing but the stems of the great plants about him receding in the violet shade, and far overhead the multiple transparency of huge leaves filtering the sunshine to the solemn splendour of twilight in which he walked. Whenever he felt able he ran again; the ground continued soft and springy, covered with the same resilient weed which was the first thing his hands had touched in Malacandra. Once or twice a small red creature scuttled across his path, but otherwise there seemed to be no life stirring in the wood; nothing to fear—except the fact of wandering unprovisioned and alone in a forest of unknown vegetation thousands or millions of miles beyond the reach or knowledge of man."
count := make(map[string]int)
for _, word := range strings.Fields(s) {
word = strings.ToLower(strings.Trim(word, ".?,"))
count[word]++
}
fmt.Println("2回以上出現した単語")
for k, v := range count {
if v >= 2 {
fmt.Printf("%-10v %2v回\n", k, v)
}
}
}
入出力結果(Zsh、PowerShell、Terminal)
% go build words.go
% ./words
2回以上出現した単語
a 2回
which 2回
but 2回
he 4回
to 3回
as 2回
far 2回
in 5回
or 3回
nothing 2回
reach 2回
and 3回
his 2回
the 12回
of 7回
% ./words
2回以上出現した単語
far 2回
the 12回
which 2回
he 4回
to 3回
or 3回
nothing 2回
as 2回
reach 2回
a 2回
his 2回
but 2回
in 5回
of 7回
and 3回
%
0 コメント:
コメントを投稿