forked from MIT-Emerging-Talent/ET6-practice-code-review
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
upload the second challenge with its test file
- Loading branch information
1 parent
3c27da4
commit 10540e0
Showing
2 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Test module for isPalindrome function in valid_palindrome.py | ||
Test categories: | ||
- Standard cases: tests typical palindromes and tests typical non-palindromes | ||
- Edge cases: empty string, single character | ||
- Special tests: Ignores spaces, punctuation and handles numbers as palindromes | ||
Created on 2024-01-12 | ||
Author: Kefah Albashityalshaer | ||
""" | ||
|
||
import unittest | ||
|
||
from ..valid_palindrome import isPalindrome | ||
|
||
|
||
class TestIsPalindromesFunction(unittest.TestCase): | ||
# Standard Test Cases (Palindromes and Non-Palindromes) | ||
def test_simple_palindrome(self): | ||
self.assertTrue(isPalindrome("madam"), "Should return True for 'madam'") | ||
self.assertTrue(isPalindrome("racecar"), "Should return True for 'racecar'") | ||
|
||
def test_simple_non_palindrome(self): | ||
self.assertFalse(isPalindrome("hello"), "Should return False for 'hello'") | ||
self.assertFalse(isPalindrome("world"), "Should return False for 'world'") | ||
|
||
# Edge Cases | ||
def test_empty_string(self): | ||
self.assertTrue(isPalindrome(""), "Should return True for an empty string") | ||
|
||
def test_single_character(self): | ||
self.assertTrue(isPalindrome("a"), "Should return True for a single character") | ||
|
||
def test_mixed_case(self): | ||
self.assertTrue(isPalindrome("MadAm"), "Should return True regardless of case") | ||
self.assertTrue( | ||
isPalindrome("RaCeCaR"), "Should return True regardless of case" | ||
) | ||
|
||
# Special Cases | ||
def test_special_characters(self): | ||
self.assertTrue( | ||
isPalindrome("A man, a plan, a canal: Panama"), | ||
"Should return True ignoring special characters", | ||
) | ||
self.assertTrue( | ||
isPalindrome("No lemon, no melon!"), | ||
"Should return True ignoring punctuation", | ||
) | ||
|
||
def test_numeric_palindrome(self): | ||
self.assertTrue( | ||
isPalindrome("12321"), "Should return True for numeric palindrome" | ||
) | ||
self.assertFalse( | ||
isPalindrome("12345"), | ||
"Should return False for non-palindrome numbers", | ||
) | ||
|
||
def test_mixed_alpha_numeric(self): | ||
self.assertTrue( | ||
isPalindrome("1a2b2a1"), | ||
"Should return True for alphanumeric palindrome", | ||
) | ||
self.assertFalse( | ||
isPalindrome("1a2b3c"), | ||
"Should return False for non-palindrome alphanumeric string", | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
A module to check if the string is a Palindrome | ||
A phrase is a `palindrome` if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, | ||
it reads the same forward and backward. Alphanumeric characters include letters and numbers. | ||
Return `true` if it is a palindrome, or `false` otherwise. | ||
Module contents: | ||
- isPalindrome: Given a string s, return true if it is a palindrome, or false otherwise | ||
Created on 2024-01-12 | ||
Author: Kefah Albashityalshaer | ||
""" | ||
|
||
|
||
def isPalindrome(s: str) -> bool: | ||
""" | ||
Function to find the `Palindrome`, which is a Alphanumeric characters that | ||
can be read the same forward and backward | ||
Takes a string with mix of Alphanumeric and non-alphanumeric, converting it to lowercase, removing | ||
all non-alphanumeric characters, and check if the result is `Palindrome` | ||
Parameters: | ||
s: str, s is a string to be checked if it's a Palindrome | ||
Return -> Bool: the return values is a boolean, True or False | ||
Examples: | ||
>>> isPalindrome("A man, a plan, a canal: Panama") | ||
True | ||
>>> isPalindrome("race a car") | ||
False | ||
>>> isPalindrome(" ") | ||
True | ||
""" | ||
assert isinstance(s, str), "input should be a string" | ||
|
||
new_s = "" | ||
for char in s: | ||
if char.isalpha() or char.isnumeric(): | ||
new_s += char.lower() | ||
return new_s == new_s[::-1] |