forked from arsho/46-Simple-Python-Exercises-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_36.py
More file actions
28 lines (26 loc) · 783 Bytes
/
problem_36.py
File metadata and controls
28 lines (26 loc) · 783 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
'''
Statement:
======================
A hapax legomenon (often abbreviated to hapax) is a word which occurs
only once in either the written
record of a language, the works of an author,
or in a single text. Define a function that given the file name of
a text will return all its hapaxes.
Make sure your program ignores capitalization.'''
import re
#filename = input("Enter a line: ")
filename = "words.txt"
fin = open(filename,"r")
lines = fin.readlines()
word_dict = dict()
for line in lines:
word_list = re.findall('\w+',line)
for word in word_list:
word = word.lower()
if word in word_dict:
word_dict[word]=word_dict[word]+1
else:
word_dict[word]=1
for word in word_dict:
if word_dict[word] == 1:
print(word)