Browse Source

clarify palindrome checks

Charles Reid 1 year 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
11 11
 from euclidean_distance import euclidean_distance
12 12
 from pprint import pprint
13 13
 
14
-def is_near_palindrome(word,tol):
15
-    test1 = euclidean_distance(word[0],word[4])<=tol
16
-    test2 = euclidean_distance(word[1],word[3])<=tol
17
-    if(test1 and test2):
14
+def is_near_palindrome(word,lo,hi):
15
+    d1 = euclidean_distance(word[0],word[4])
16
+    d2 = euclidean_distance(word[1],word[3])
17
+
18
+    if( (d1+d2) > lo and (d1+d2) <= hi ):
18 19
         return True
20
+
19 21
     return False
20 22
 
21 23
 if __name__=="__main__":
@@ -25,14 +27,16 @@ if __name__=="__main__":
25 27
     near_palindromes = []
26 28
 
27 29
     # Euclidean distance tolerance
28
-    tol = 2.0
30
+    lo = 0.0
31
+    hi = 1.0
29 32
 
30 33
     for i in range(len(words)):
31
-        if(is_near_palindrome(words[i],tol)):
34
+        if(is_near_palindrome(words[i],lo,hi)):
32 35
             knp += 1
33 36
             near_palindromes.append(words[i])
34 37
 
35 38
     print("-"*40)
36 39
     print("Near Palindromes: \n")
37
-    pprint(near_palindromes)
40
+    print(", ".join(near_palindromes))
41
+    print("The number of near-palindromes is {0:d}".format(len(near_palindromes)))
38 42
 

+ 5
- 3
palindromes.py View File

@@ -44,7 +44,8 @@ if __name__=="__main__":
44 44
 
45 45
     print("-"*40)
46 46
     print("Palindromes: \n")
47
-    pprint(palindromes)
47
+    print(", ".join(palindromes))
48
+    print("There are {0:d} palindromes.".format(kp))
48 49
 
49 50
     # Check for palindrome pairs
50 51
     for i in range(len(words)):
@@ -57,5 +58,6 @@ if __name__=="__main__":
57 58
 
58 59
     print("-"*40)
59 60
     print("Palindrome Pairs: \n")
60
-    pprint(palindrome_pairs)
61
-            
61
+    for pair in palindrome_pairs:
62
+        print(", ".join(pair))
63
+    print("There are {0:d} palindrome pairs.".format(kpp))