21 lines
709 B
Mojo
21 lines
709 B
Mojo
# probe: splitting text and counting words in a Dict, in insertion order
|
|
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
|
|
# expect-stdout: the 3
|
|
# expect-stdout: cat 1
|
|
# expect-stdout: and 2
|
|
# expect-stdout: hat 1
|
|
# expect-stdout: bat 1
|
|
# expect-stdout: distinct: 5
|
|
def main() raises:
|
|
var text = String("the cat and the hat and the bat")
|
|
var counts = Dict[String, Int]()
|
|
for w in text.split(" "):
|
|
var word = String(w)
|
|
if word in counts:
|
|
counts[word] += 1
|
|
else:
|
|
counts[word] = 1
|
|
for e in counts.items():
|
|
print(e.key, e.value)
|
|
print("distinct:", len(counts))
|