Finished hw 2

This commit is contained in:
2026-01-25 23:44:19 -05:00
parent dd9cb587aa
commit a49b1c0d4a
4 changed files with 41 additions and 0 deletions

28
HW 2/dcs_211_hw2.py Normal file
View File

@@ -0,0 +1,28 @@
def parseFile(filename):
word_counts = {}
file = open(filename, 'r')
line = file.readline()
while line != "":
word=line.strip()
if word in word_counts:
word_counts[word] += 1
else:
word_counts[word] = 1
line = file.readline()
file.close()
return word_counts
def main():
print("Test 1: test1.txt")
print(parseFile("test1.txt"))
print()
print("Test 2: test2.txt")
print(parseFile("test2.txt"))
print()
print("Test 3: test3.txt")
print(parseFile("test3.txt"))
print()
if __name__ == "__main__":
main()