テキストファイルを取り込んで解析するメソッドとのインターフェースを持つパッケージを実装する必要があります。合計文字数をカウントし、最も頻繁に出現するシンボルと単語を見つけます。最も頻繁に現れる文字を見つけるには、テキスト中の各ルーンをループし、文字列に変換してmap
のキーとして追加します。値はインクリメントされたカウンタで、この文字が指定されたテキストでどのくらいの頻度で発生したかをカウントします。今私は次の問題で少し立ち往生しています - 私はマップ内で最高の価値を持つキーを取得する方法を理解できません。ここでは、コードです:テキスト内で最も頻繁に出現する文字を見つける
package textscanner
import (
"fmt"
"log"
"io/ioutil"
"unicode/utf8"
"strconv"
)
// Initializing my scanner
type Scanner interface {
countChar(text string) int
frequentSym(text string) // Return value is not yet implemented
Scan()
Run()
}
/* method counting characters */
func countChar(sc Scanner, text string) int { ... }
func frequentSym(sc Scanner, text string) {
// Make a map with string key and integer value
symbols := make(map[string] int)
// Iterate through each char in text
for _, sym := range text {
// Convert rune to string
char := strconv.QuoteRune(sym)
// Set this string as a key in map and assign a counter value
count := symbols[char]
if count == symbols[char] {
// increment the value
symbols[char] = count + 1
} else {
symbols[char] = 1
}
}
}
だから、基本的に私は最高int
値のペアを見つけると、それに対応string
キーを返却する必要があり、それがテキスト
なぜあなたは文字列に変換するのですか?あなたはmapキーとしてruneを使うことはできません。つまり 'map [rune] int'ですか? – ain