created count_words.py
This commit is contained in:
BIN
Lab 1/.DS_Store
vendored
Normal file
BIN
Lab 1/.DS_Store
vendored
Normal file
Binary file not shown.
BIN
Lab 1/Screenshot 1.png
Normal file
BIN
Lab 1/Screenshot 1.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 849 KiB |
BIN
Lab 1/Screenshot 2.png
Normal file
BIN
Lab 1/Screenshot 2.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 849 KiB |
28
Lab 1/count_words.py
Normal file
28
Lab 1/count_words.py
Normal 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()
|
||||||
7
Lab 1/words.txt
Normal file
7
Lab 1/words.txt
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
cat
|
||||||
|
dog
|
||||||
|
cat
|
||||||
|
benjamin
|
||||||
|
apple
|
||||||
|
house
|
||||||
|
benjamin
|
||||||
7
Lab 1/words_backup.txt
Normal file
7
Lab 1/words_backup.txt
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
cat
|
||||||
|
dog
|
||||||
|
cat
|
||||||
|
benjamin
|
||||||
|
apple
|
||||||
|
house
|
||||||
|
benjamin
|
||||||
BIN
globbing/.DS_Store
vendored
Normal file
BIN
globbing/.DS_Store
vendored
Normal file
Binary file not shown.
0
globbing/myfile
Normal file
0
globbing/myfile
Normal file
0
globbing/myfile1
Normal file
0
globbing/myfile1
Normal file
0
globbing/myfile123
Normal file
0
globbing/myfile123
Normal file
0
globbing/myfile1a
Normal file
0
globbing/myfile1a
Normal file
0
globbing/myfile1b
Normal file
0
globbing/myfile1b
Normal file
0
globbing/myfile1c
Normal file
0
globbing/myfile1c
Normal file
0
globbing/myfile2
Normal file
0
globbing/myfile2
Normal file
2
globbing/permissions/backup/words.txt
Normal file
2
globbing/permissions/backup/words.txt
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
hello
|
||||||
|
world
|
||||||
2
globbing/permissions/words.txt
Normal file
2
globbing/permissions/words.txt
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
hello
|
||||||
|
world
|
||||||
0
globbing/yourfile
Normal file
0
globbing/yourfile
Normal file
0
globbing/yourfile1
Normal file
0
globbing/yourfile1
Normal file
0
globbing/yourfile123
Normal file
0
globbing/yourfile123
Normal file
0
globbing/yourfile1a
Normal file
0
globbing/yourfile1a
Normal file
0
globbing/yourfile2
Normal file
0
globbing/yourfile2
Normal file
Reference in New Issue
Block a user