From a966dca1d07483e8b402a4b47f144bc8698a2b12 Mon Sep 17 00:00:00 2001 From: PJ Passalacqua Date: Wed, 27 May 2015 11:54:33 -0400 Subject: [PATCH 01/14] added test_file --- test_file | 1 + 1 file changed, 1 insertion(+) create mode 100644 test_file diff --git a/test_file b/test_file new file mode 100644 index 0000000..73709ba --- /dev/null +++ b/test_file @@ -0,0 +1 @@ +Testing From e5f9aae543a27d43b7a0c3043dfc91212d196246 Mon Sep 17 00:00:00 2001 From: PJ Passalacqua Date: Wed, 27 May 2015 12:59:33 -0400 Subject: [PATCH 02/14] removed test_file --- test_file | 1 - 1 file changed, 1 deletion(-) delete mode 100644 test_file diff --git a/test_file b/test_file deleted file mode 100644 index 73709ba..0000000 --- a/test_file +++ /dev/null @@ -1 +0,0 @@ -Testing From 52644b67466d8bad8319f18798dc4d8ff0e7ee64 Mon Sep 17 00:00:00 2001 From: PJ Passalacqua Date: Wed, 27 May 2015 13:01:24 -0400 Subject: [PATCH 03/14] added plaindrome.py --- palindrome.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 palindrome.py diff --git a/palindrome.py b/palindrome.py new file mode 100644 index 0000000..e69de29 From 6d88b6a5d1c82c521dad35f12cf219f4cb5d7840 Mon Sep 17 00:00:00 2001 From: PJ Passalacqua Date: Wed, 27 May 2015 13:16:32 -0400 Subject: [PATCH 04/14] added pseudocode --- palindrome.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/palindrome.py b/palindrome.py index e69de29..c0da77b 100644 --- a/palindrome.py +++ b/palindrome.py @@ -0,0 +1,8 @@ +# Get user input. + +# Remove spaces, punctuation, and capitalization from the input + +# Algorithm + # Check to see if first and last characters are the same + # If they are, continue + # If they aren't, break From 51f3d5f63a8790466770320b3f549ac566e143a9 Mon Sep 17 00:00:00 2001 From: PJ Passalacqua Date: Wed, 27 May 2015 13:31:56 -0400 Subject: [PATCH 05/14] created palindrome_check function --- palindrome.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/palindrome.py b/palindrome.py index c0da77b..1678957 100644 --- a/palindrome.py +++ b/palindrome.py @@ -1,3 +1,15 @@ +# Functions + + +def palindrome_check(string_): + if len(string_) < 2: + return True + if string_.endswith(string_[0]): + return palindrome_check(string_[1:-1]) + return False + + + # Get user input. # Remove spaces, punctuation, and capitalization from the input From bdd8a56be1446cbff1140ad3ca10de48c1b554a9 Mon Sep 17 00:00:00 2001 From: PJ Passalacqua Date: Wed, 27 May 2015 13:47:06 -0400 Subject: [PATCH 06/14] Added comment for palindrome_check --- palindrome.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/palindrome.py b/palindrome.py index 1678957..23a8294 100644 --- a/palindrome.py +++ b/palindrome.py @@ -1,6 +1,6 @@ # Functions - +# Takes a string and returns True if the string is a palindrome. def palindrome_check(string_): if len(string_) < 2: return True From 50c97c94e0d505d0d4d28aa0fde813944cc00a67 Mon Sep 17 00:00:00 2001 From: PJ Passalacqua Date: Wed, 27 May 2015 14:12:16 -0400 Subject: [PATCH 07/14] added print statements for each step of palindrome_check --- palindrome.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/palindrome.py b/palindrome.py index 23a8294..81d02b2 100644 --- a/palindrome.py +++ b/palindrome.py @@ -1,11 +1,18 @@ # Functions # Takes a string and returns True if the string is a palindrome. -def palindrome_check(string_): +def palindrome_check_recursive(string_): + print("Checking remaining length.") if len(string_) < 2: + print("Remaining string is less than 2 characters.") return True + print("Remaining string is {}.".format(string_)) + print("Comparing first and last characters.") if string_.endswith(string_[0]): + print("First and last characters are the same.") + print("Stripping first and last characters from the string.") return palindrome_check(string_[1:-1]) + print("First and last characters are different.") return False From de61a07312cb56aa6d37ebe5860c3392323bf639 Mon Sep 17 00:00:00 2001 From: PJ Passalacqua Date: Wed, 27 May 2015 14:20:39 -0400 Subject: [PATCH 08/14] added reformat function --- palindrome.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/palindrome.py b/palindrome.py index 81d02b2..78cf309 100644 --- a/palindrome.py +++ b/palindrome.py @@ -1,3 +1,6 @@ +import re + + # Functions # Takes a string and returns True if the string is a palindrome. @@ -15,6 +18,11 @@ def palindrome_check_recursive(string_): print("First and last characters are different.") return False +# Takes a string and returns all of the letters in lower case. +def reformat(string_): + string_ = re.sub(r'[^A-Za-z]','',string_) + string_ = string_.lower() + return string_ # Get user input. From 7beb16a9e8b0f322a35a544195a6b665bb97d142 Mon Sep 17 00:00:00 2001 From: PJ Passalacqua Date: Wed, 27 May 2015 15:21:16 -0400 Subject: [PATCH 09/14] implemented palindrom_check and reformat into full program --- palindrome.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/palindrome.py b/palindrome.py index 78cf309..0d6cd27 100644 --- a/palindrome.py +++ b/palindrome.py @@ -14,22 +14,36 @@ def palindrome_check_recursive(string_): if string_.endswith(string_[0]): print("First and last characters are the same.") print("Stripping first and last characters from the string.") - return palindrome_check(string_[1:-1]) + return palindrome_check_recursive(string_[1:-1]) print("First and last characters are different.") return False # Takes a string and returns all of the letters in lower case. def reformat(string_): + print("Removing all non-letters.") string_ = re.sub(r'[^A-Za-z]','',string_) + print("Making string lower case.") string_ = string_.lower() return string_ # Get user input. +potential_palindrome_input = input("Enter a string > ") + +if not potential_palindrome_input: + print ("You didn't enter anything.") + exit() # Remove spaces, punctuation, and capitalization from the input +potential_palindrome = reformat(potential_palindrome_input) # Algorithm - # Check to see if first and last characters are the same - # If they are, continue - # If they aren't, break + # Compare first and last letter. + # If they are the same, then compare next letters +print("Checking to see if you entered a palindrome.") +is_a_palindrome = palindrome_check_recursive(potential_palindrome) + +if is_a_palindrome: + print("{} is a palindrome.".format(potential_palindrome_input)) +else: + print("{} is not a palindrome.".format(potential_palindrome)) From 7b6ec8020ae80d7024d2f19314fb5778908ef93e Mon Sep 17 00:00:00 2001 From: PJ Passalacqua Date: Wed, 27 May 2015 15:21:54 -0400 Subject: [PATCH 10/14] changed test.sh to use Python3 --- test.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test.sh b/test.sh index 9e22849..35a3ab8 100755 --- a/test.sh +++ b/test.sh @@ -2,7 +2,7 @@ describe "palindrome.py: Determines if a text is palindromic" -prog="python palindrome.py" +prog="python3 palindrome.py" it_works_for_even_numbers() { out="$(echo 'toot' | $prog)" From 94cd9085c212297b422ae2020d66ab015aa07adf Mon Sep 17 00:00:00 2001 From: PJ Passalacqua Date: Wed, 27 May 2015 15:57:51 -0400 Subject: [PATCH 11/14] Changed printed output --- palindrome.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/palindrome.py b/palindrome.py index 0d6cd27..b286f55 100644 --- a/palindrome.py +++ b/palindrome.py @@ -3,26 +3,24 @@ # Functions +# Prints string_ +def print_string(string_): + print("Current string: {}".format(string_)) + # Takes a string and returns True if the string is a palindrome. def palindrome_check_recursive(string_): - print("Checking remaining length.") + print_string(string_) if len(string_) < 2: - print("Remaining string is less than 2 characters.") return True - print("Remaining string is {}.".format(string_)) - print("Comparing first and last characters.") if string_.endswith(string_[0]): - print("First and last characters are the same.") - print("Stripping first and last characters from the string.") return palindrome_check_recursive(string_[1:-1]) - print("First and last characters are different.") return False # Takes a string and returns all of the letters in lower case. def reformat(string_): - print("Removing all non-letters.") + print_string(string_) string_ = re.sub(r'[^A-Za-z]','',string_) - print("Making string lower case.") + print_string(string_) string_ = string_.lower() return string_ @@ -38,9 +36,7 @@ def reformat(string_): potential_palindrome = reformat(potential_palindrome_input) # Algorithm - # Compare first and last letter. - # If they are the same, then compare next letters -print("Checking to see if you entered a palindrome.") +print_string(potential_palindrome) is_a_palindrome = palindrome_check_recursive(potential_palindrome) if is_a_palindrome: From 0dd45c36ab2dc67303690c2b2917a878963b36ec Mon Sep 17 00:00:00 2001 From: PJ Passalacqua Date: Wed, 27 May 2015 16:23:27 -0400 Subject: [PATCH 12/14] added simple check and iterative check --- palindrome.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/palindrome.py b/palindrome.py index b286f55..d5b6da7 100644 --- a/palindrome.py +++ b/palindrome.py @@ -18,15 +18,26 @@ def palindrome_check_recursive(string_): # Takes a string and returns all of the letters in lower case. def reformat(string_): - print_string(string_) string_ = re.sub(r'[^A-Za-z]','',string_) print_string(string_) string_ = string_.lower() + print_string(string_) return string_ +def palindrome_check(string_): + print_string(string_[::-1]) + return string_ == string_[::-1] + +def palindrome_check_iterative(string_): + for index, letter in enumerate(string_[::-1]): + if letter == string_[index]: + continue + return False + return True # Get user input. potential_palindrome_input = input("Enter a string > ") +print_string(potential_palindrome_input) if not potential_palindrome_input: print ("You didn't enter anything.") @@ -36,8 +47,9 @@ def reformat(string_): potential_palindrome = reformat(potential_palindrome_input) # Algorithm -print_string(potential_palindrome) is_a_palindrome = palindrome_check_recursive(potential_palindrome) +is_a_palindrome = palindrome_check(potential_palindrome) +is_a_palindrome = palindrome_check_iterative(potential_palindrome) if is_a_palindrome: print("{} is a palindrome.".format(potential_palindrome_input)) From dd496023be1f7fdda0d45559e3a2bf009f678924 Mon Sep 17 00:00:00 2001 From: PJ Passalacqua Date: Wed, 27 May 2015 16:37:34 -0400 Subject: [PATCH 13/14] required all checks to agree --- palindrome.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/palindrome.py b/palindrome.py index d5b6da7..e9f5534 100644 --- a/palindrome.py +++ b/palindrome.py @@ -46,12 +46,18 @@ def palindrome_check_iterative(string_): # Remove spaces, punctuation, and capitalization from the input potential_palindrome = reformat(potential_palindrome_input) -# Algorithm -is_a_palindrome = palindrome_check_recursive(potential_palindrome) +# Algorithms +is_a_palindrome_recursive = palindrome_check_recursive(potential_palindrome) is_a_palindrome = palindrome_check(potential_palindrome) -is_a_palindrome = palindrome_check_iterative(potential_palindrome) +is_a_palindrome_iterative = palindrome_check_iterative(potential_palindrome) -if is_a_palindrome: +if (is_a_palindrome and + is_a_palindrome_iterative and + is_a_palindrome_recursive): print("{} is a palindrome.".format(potential_palindrome_input)) -else: +elif (not is_a_palindrome and + not is_a_palindrome_iterative and + not is_a_palindrome_recursive): print("{} is not a palindrome.".format(potential_palindrome)) +else: + print("Had an inconsistent answer.") From 85af4c14b06a1facf9a5138bf2a9d9458ec8e53d Mon Sep 17 00:00:00 2001 From: PJ Passalacqua Date: Thu, 28 May 2015 09:03:22 -0400 Subject: [PATCH 14/14] added comments to palindrome checks --- palindrome.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/palindrome.py b/palindrome.py index e9f5534..bb55c56 100644 --- a/palindrome.py +++ b/palindrome.py @@ -2,7 +2,6 @@ # Functions - # Prints string_ def print_string(string_): print("Current string: {}".format(string_)) @@ -24,10 +23,12 @@ def reformat(string_): print_string(string_) return string_ +# Takes a string and returns True if the string is a palindrome. def palindrome_check(string_): print_string(string_[::-1]) return string_ == string_[::-1] +# Takes a string and returns True if the string is a palindrome. def palindrome_check_iterative(string_): for index, letter in enumerate(string_[::-1]): if letter == string_[index]: @@ -35,6 +36,7 @@ def palindrome_check_iterative(string_): return False return True +# Main program. # Get user input. potential_palindrome_input = input("Enter a string > ") print_string(potential_palindrome_input) @@ -51,6 +53,7 @@ def palindrome_check_iterative(string_): is_a_palindrome = palindrome_check(potential_palindrome) is_a_palindrome_iterative = palindrome_check_iterative(potential_palindrome) +# Results if (is_a_palindrome and is_a_palindrome_iterative and is_a_palindrome_recursive): @@ -58,6 +61,6 @@ def palindrome_check_iterative(string_): elif (not is_a_palindrome and not is_a_palindrome_iterative and not is_a_palindrome_recursive): - print("{} is not a palindrome.".format(potential_palindrome)) + print("{} is not a palindrome.".format(potential_palindrome_input)) else: print("Had an inconsistent answer.")