Finished hw 2
This commit is contained in:
28
HW 2/dcs_211_hw2.py
Normal file
28
HW 2/dcs_211_hw2.py
Normal 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()
|
||||||
6
HW 2/test1.txt
Normal file
6
HW 2/test1.txt
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
apple
|
||||||
|
banana
|
||||||
|
apple
|
||||||
|
orange
|
||||||
|
banana
|
||||||
|
apple
|
||||||
4
HW 2/test2.txt
Normal file
4
HW 2/test2.txt
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
dog
|
||||||
|
cat
|
||||||
|
bird
|
||||||
|
fish
|
||||||
3
HW 2/test3.txt
Normal file
3
HW 2/test3.txt
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
python
|
||||||
|
python
|
||||||
|
python
|
||||||
Reference in New Issue
Block a user