blob: dfb7dac2359bc44d7dae521312a433b159342a87 (
plain)
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
|
#!/usr/bin/env python3
# Challenge 005
#
# Challenge #1
# Write a program which prints out all anagrams for a given word. For more
# information about Anagram, please check this wikipedia page.
# create a hash of all words in dictionary where key is sorted list of letters
# therefore two anagrams have the same key
import sys
import re
def read_file(filename):
with open(filename) as f:
return f.readlines()
def read_words(lines):
words = []
for line in lines:
word = line.strip()
if not re.search(r"\W", word):
words.append(word)
return words
def get_word_key(word):
letters = sorted([x for x in word.lower()])
return "".join(letters)
def print_anagrams(myword):
myword_key = get_word_key(myword)
for word in read_words(read_file("words.txt")):
if get_word_key(word)==myword_key:
print(word.lower())
print_anagrams(sys.argv[1])
|