-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanagrams.py
76 lines (61 loc) · 1.98 KB
/
anagrams.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import sys
def getOrderedStr(str):
"""
:param str: a String
:return: lexographical ordering of this string in lower case
"""
return ''.join(sorted(str.lower()))
def parseDict(fileLoc):
"""
:param fileLoc: location of txt file with one word per line
:return: a dictionary of sets mapping an ordered lowercase string to all words with
those exact characters. returns None if unable to open the file.
"""
#tries to open the file. returns None if unsuccessful
try:
filep = open(fileLoc, 'r')
except "FileNotFoundError":
return None
if filep == None: return None
dict = {}
#iterates the file, pulls out a word, gets its ordering
# and puts the ordering in the dictionary to return
for line in filep:
line = line.strip('\n')
if len(line) > 0:
orderedStr = getOrderedStr(line)
if orderedStr not in dict:
dict[orderedStr] = set([])
dict[orderedStr].add(line)
return dict;
def getAnagrams(dict, str):
"""
:param dict: a dictionary mapping a string ordering to a set of words.
should be the return value from parseDict
:param str: a String to get the anagram for
:return: "-" if no anagram matches found in dictionary or
a string containing all words spaced out
"""
orderedStr = getOrderedStr(str)
if orderedStr in dict:
anagramSet = dict[orderedStr]
return " ".join(sorted(list(anagramSet)))
else: return "-"
def main():
#obtains the dictionary
if len(sys.argv) < 2:
print "error: need dictionary location"
return
else:
dict = parseDict(sys.argv[1])
if dict is None:
print "could not parse file"
return
#waits for input from user. will exit on empty string
while(True):
line = raw_input(">").strip('\n')
if line == "":
break;
print getAnagrams(dict, line)
if __name__ == "__main__":
main()