I'm new to Python 3, and I tried to test if my palindrome code works and passes most of the edge cases, so I created the following 2 Python files. When I ran the python3 test_palindromes.py
on the terminal, it shows that 2 tests passed. And I was wondering if that is sufficient for my palindrome function. I think I have exhausted all of the cases.
Test cases for Python palindrome:
palindromes.py
#!python
# Hint: use string.ascii_letters (all letters in ASCII character set)
import string
def is_palindrome(text):
"""A string of characters is a palindrome if it reads the same forwards and
backwards, ignoring punctuation, whitespace, and letter casing"""
# implement is_palindrome_iterative and is_palindrome_recursive below, then
# change this to call your implementation to verify it passes all tests
assert isinstance(text, str)
return is_palindrome_iterative(text)
# return is_palindrome_recursive(text)
def is_palindrome_iterative(text):
# TODO: implement the is_palindrome function iteratively here
# once implemented, change is_palindrome to call is_palindrome_iterative
# to verify that your iterative implementation passes all tests
"""str.isalpha()
Return true if all characters in the string are alphabetic and there is at
least one character, false otherwise. Alphabetic characters are those
characters defined in the Unicode character database as “Letter”, i.e.,
those with general category property being one of “Lm”, “Lt”, , “Ll”, or “Lo”.
Note that this is different from the “Alphabetic” property
defined in the Unicode Standard. (edited)"""
# First, setting up 2 pointer. First and last pointer.
first_pointer = 0
last_pointer = len(text) - 1
# iteration through when the first index is less than the last index
while(first_pointer <= last_pointer):
# set up different while loop condition to do comparison
# test different condition of the palindrome cases
#
# Get letters only
while not text[first_pointer].isalpha():
first_pointer += 1
if first_pointer > len(text) - 1:
return True
while not text[last_pointer].isalpha():
last_pointer -= 1
if last_pointer < 0:
return True
# Not same, return
if(text[first_pointer].lower() != text[last_pointer].lower()):
return False
first_pointer += 1
last_pointer -= 1
return True
def main():
import sys
args = sys.argv[1:] # Ignore script file name
if len(args) > 0:
for arg in args:
is_pal = is_palindrome(arg)
result = 'PASS' if is_pal else 'FAIL'
str_not = 'a' if is_pal else 'not a'
print('{}: {} is {} palindrome'.format(result, repr(arg), str_not))
else:
print('Usage: {} string1 string2 ... stringN'.format(sys.argv[0]))
print(' checks if each argument given is a palindrome')
if __name__ == '__main__':
main()
test_palindrome.py
import unittest
class TestPalindromes(unittest.TestCase):
def test_is_palindrome_with_mirrored_strings(self):
# simple palindromes that are mirrored strings
assert is_palindrome('') is True # base case
assert is_palindrome('A') is True # base case
assert is_palindrome('BB') is True
assert is_palindrome('LOL') is True
assert is_palindrome('noon') is True
assert is_palindrome('radar') is True
assert is_palindrome('racecar') is True
# Enable each of these test cases by deleting DISABLED_
def DISABLED_test_is_palindrome_with_mixed_casing(self):
# palindromes with mixed leter casing
assert is_palindrome('Bb') is True
assert is_palindrome('NoOn') is True
assert is_palindrome('Radar') is True
assert is_palindrome('RaceCar') is True
def DISABLED_test_is_palindrome_with_whitespace(self):
# palindromes with whitespace
assert is_palindrome('taco cat') is True
assert is_palindrome('race car') is True
assert is_palindrome('race fast safe car') is True
def DISABLED_test_is_palindrome_with_whitespace_and_mixed_casing(self):
# palindromes with whitespace and mixed letter casing
assert is_palindrome('Taco Cat') is True
assert is_palindrome('Race Car') is True
assert is_palindrome('Race Fast Safe Car') is True
def DISABLED_test_is_palindrome_with_whitespace_and_punctuation(self):
# palindromes with whitespace and punctuation
assert is_palindrome('taco cat!') is True
assert is_palindrome('race, car!!') is True
assert is_palindrome('race fast, safe car.') is True
def DISABLED_test_is_palindrome_with_mixed_casing_and_punctuation(self):
# palindromes with whitespace, punctuation and mixed letter casing
assert is_palindrome('Race fast, safe car.') is True
assert is_palindrome('Was it a car or a cat I saw?') is True
assert is_palindrome("Go hang a salami, I'm a lasagna hog.") is True
assert is_palindrome('A man, a plan, a canal - Panama!') is True
def test_is_palindrome_with_non_palindromic_strings(self):
assert is_palindrome('AB') is False # even length
assert is_palindrome('ABC') is False # odd length
assert is_palindrome('doge') is False
assert is_palindrome('monkey') is False
assert is_palindrome('chicken, monkey!') is False
if __name__ == '__main__':
unittest.main()