created count_words.py
This commit is contained in:
1 parent
a49b1c0d4a
commit
9af5ad39e4
23 files changed
+46
No files matched your search
Vendored
BIN
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 849 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 849 KiB |
@@ -0,0 +1,28 @@
|
||||
def countWords(filename: str) -> dict[str, int]:
|
||||
word_counts: dict[str, int] = {}
|
||||
file = open(filename, "r")
|
||||
line = file.readline()
|
||||
|
||||
while line != "":
|
||||
line = line.strip()
|
||||
words = line.split()
|
||||
for word in words:
|
||||
if word in word_counts:
|
||||
word_counts[word] += 1
|
||||
else:
|
||||
word_counts[word] = 1
|
||||
|
||||
line = file.readline()
|
||||
|
||||
file.close()
|
||||
return word_counts
|
||||
|
||||
|
||||
def main() -> None:
|
||||
filename = "words.txt"
|
||||
counts = countWords(filename)
|
||||
print(counts)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,7 @@
|
||||
cat
|
||||
dog
|
||||
cat
|
||||
benjamin
|
||||
apple
|
||||
house
|
||||
benjamin
|
||||
@@ -0,0 +1,7 @@
|
||||
cat
|
||||
dog
|
||||
cat
|
||||
benjamin
|
||||
apple
|
||||
house
|
||||
benjamin
|
||||
Reference in new issue
Block a user