Use count tables for centerpiece example
This commit is contained in:
parent
d812b28505
commit
ffafafa239
1 changed files with 3 additions and 10 deletions
|
|
@ -11,18 +11,11 @@ Getting started? Head over to the [installation instructions](/getting_started/)
|
||||||
``` nimrod
|
``` nimrod
|
||||||
import tables, strutils
|
import tables, strutils
|
||||||
|
|
||||||
var wordFrequencies = initTable[string, int]()
|
var wordFrequencies = newCountTable[string]()
|
||||||
|
|
||||||
for line in stdin.lines:
|
for line in stdin.lines:
|
||||||
for word in line.split(", "):
|
for word in line.split(", "):
|
||||||
wordFrequencies[word] = wordFrequencies[word] + 1
|
wordFrequencies.inc(word)
|
||||||
|
|
||||||
var maxFrequency = 0
|
echo "The most frequent word is '", wordFrequencies.largest, "'"
|
||||||
var mostFrequentWord: string
|
|
||||||
for word, frequency in wordFrequencies:
|
|
||||||
if frequency > maxFrequency:
|
|
||||||
maxFrequency = frequency
|
|
||||||
mostFrequentWord = word
|
|
||||||
|
|
||||||
echo "The most frequent word is '", mostFrequentWord, "'"
|
|
||||||
```
|
```
|
||||||
Loading…
Add table
Add a link
Reference in a new issue