forked from sd18fall/TimingProfiling
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmirror_pairs.py
More file actions
67 lines (54 loc) · 1.8 KB
/
mirror_pairs.py
File metadata and controls
67 lines (54 loc) · 1.8 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
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
"""
Find mirror pairs of words from a word list.
Mirror pairs exist when a valid word can be formed both forward and backward
e.g. tuba <-> abut
Palindromes are a special case of mirror pairs, and mirror pairs can be
useful in constructing palindromic phrases.
"""
def reverse_word(word):
"""
Return a string with characters in 'word' reversed.
>>> reverse_word("software")
'erawtfos'
"""
backwards_word = ""
for i in range(len(word)):
backwards_word += word[len(word)-1-i]
return backwards_word
def has_mirror_pair(word, all_words):
"""
Given a 'word' to check and a full set of 'all_words',
return True if 'word' has a mirror pair within 'all_words'.
"""
for candidate in all_words:
if reverse_word(word) == candidate:
return True
return False
def find_mirror_pairs(word_list, max_search=-1):
"""
Return a list of all words in 'word_list' that have a mirror
pair within the word_list.
If 'max_search' is given, only look for mirror pairs for the
first 'max_search' words; otherwise search full 'word_list'.
"""
# BUG: For some reason this doesn't search the whole word_list
# if I don't provide max_search - figure it out later.
pairs = []
for word in word_list[:max_search]:
if has_mirror_pair(word, word_list):
pairs.append(word)
return pairs
def get_wordlist(filename):
"""
Return a list of words from 'filename', which must be a word
list text file with one word per line.
"""
words = []
with open(filename, "r") as fp:
for line in fp:
words.append(line.strip())
return words
# Run if called from the command line
if __name__ == "__main__":
words = get_wordlist("words.txt")
print(find_mirror_pairs(words, 100))