forked from kennyyu/bootcamp-python
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathdictionary.py
More file actions
42 lines (37 loc) · 1.03 KB
/
dictionary.py
File metadata and controls
42 lines (37 loc) · 1.03 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""
We represent an English dictionary by using a python set
(don't confuse our English definition of dictionary
with a python dictionary here--anytime you see the word
"dictionary", we mean English dictionary).
"""
def load(dictionary_name):
"""
Opens the file called `dictionary_name` and returns
the set of words in that file.
Hint: call the strip() method on a word to trim surrounding
whitespace and newlines.
Each line in the file contains exactly one word.
"""
f = open(dictionary_name)
lines = f.readlines()
words = set()
for x in lines:
stripped = x.strip
words.add(stripped)
f.close()
return words
def check(dictionary, word):
"""
Returns True if `word` is in the English `dictionary`.
"""
return word in dictionary
def size(dictionary):
"""
Returns the number of words in the English `dictionary`.
"""
return len(dictionary)
def unload(dictionary):
"""
Removes everything from the English `dictionary`.
"""
dictionary.clear