-
Notifications
You must be signed in to change notification settings - Fork 302
/
near_palindromes.py
42 lines (31 loc) · 1014 Bytes
/
near_palindromes.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
"""
near_palindromes.py
Donald Knuth, Art of Computer Programming, Volume 4 Facsimile 0
Variation on Exercise #29
Find SGB words that are near-palindromes
(edit distance of one or two letters away from a palindrome).
"""
from get_words import get_words
from euclidean_distance import euclidean_distance
from pprint import pprint
def is_near_palindrome(word,lo,hi):
d1 = euclidean_distance(word[0],word[4])
d2 = euclidean_distance(word[1],word[3])
if( (d1+d2) > lo and (d1+d2) <= hi ):
return True
return False
if __name__=="__main__":
words = get_words()
knp = 0
near_palindromes = []
# Euclidean distance tolerance
lo = 0.0
hi = 1.0
for i in range(len(words)):
if(is_near_palindrome(words[i],lo,hi)):
knp += 1
near_palindromes.append(words[i])
print("-"*40)
print("Near Palindromes: \n")
print(", ".join(near_palindromes))
print("The number of near-palindromes is {0:d}".format(len(near_palindromes)))