Browse Source

clarify palindrome checks

master
Charles Reid 2 years ago
parent
commit
907676dd82
2 changed files with 16 additions and 10 deletions
  1. +11
    -7
      near_palindromes.py
  2. +5
    -3
      palindromes.py

+ 11
- 7
near_palindromes.py View File

@@ -11,11 +11,13 @@ from get_words import get_words
from euclidean_distance import euclidean_distance
from pprint import pprint

def is_near_palindrome(word,tol):
test1 = euclidean_distance(word[0],word[4])<=tol
test2 = euclidean_distance(word[1],word[3])<=tol
if(test1 and test2):
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__":
@@ -25,14 +27,16 @@ if __name__=="__main__":
near_palindromes = []

# Euclidean distance tolerance
tol = 2.0
lo = 0.0
hi = 1.0

for i in range(len(words)):
if(is_near_palindrome(words[i],tol)):
if(is_near_palindrome(words[i],lo,hi)):
knp += 1
near_palindromes.append(words[i])

print("-"*40)
print("Near Palindromes: \n")
pprint(near_palindromes)
print(", ".join(near_palindromes))
print("The number of near-palindromes is {0:d}".format(len(near_palindromes)))


+ 5
- 3
palindromes.py View File

@@ -44,7 +44,8 @@ if __name__=="__main__":

print("-"*40)
print("Palindromes: \n")
pprint(palindromes)
print(", ".join(palindromes))
print("There are {0:d} palindromes.".format(kp))

# Check for palindrome pairs
for i in range(len(words)):
@@ -57,5 +58,6 @@ if __name__=="__main__":

print("-"*40)
print("Palindrome Pairs: \n")
pprint(palindrome_pairs)
for pair in palindrome_pairs:
print(", ".join(pair))
print("There are {0:d} palindrome pairs.".format(kpp))

Loading…
Cancel
Save