-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathanagram_match.py
More file actions
31 lines (27 loc) · 801 Bytes
/
anagram_match.py
File metadata and controls
31 lines (27 loc) · 801 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
29
30
31
import sys
# Return a list of words in a given file.
def read_words(file):
words = []
with open(file) as file:
for line in file:
line = line.rstrip('\n')
words.append(line)
return words
# Sort the characters in a given string.
def sort(string):
return ''.join(sorted(list(string)))
def main(word_file, string):
sorted_string = sort(string)
words = read_words(word_file)
found = False
for word in words:
if word != string and sort(word) == sorted_string:
print("Found: %s" % word)
found = True
if not found:
print("Not found")
if __name__ == "__main__":
if len(sys.argv) != 3:
print("usage: %s word_file string" % sys.argv[0])
exit(1)
main(sys.argv[1], sys.argv[2])