created count_words.py

This commit is contained in:
2026-01-29 15:59:41 -05:00
parent a49b1c0d4a
commit 9af5ad39e4
23 changed files with 46 additions and 0 deletions

28
Lab 1/count_words.py Normal file
View File

@@ -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()