Skip to content

Commit

Permalink
Merge pull request #43 from MIT-Emerging-Talent/is_palindrome
Browse files Browse the repository at this point in the history
is_palindrome challenge
  • Loading branch information
safaabuzaid authored Jan 11, 2025
2 parents 857c0ba + f729ab2 commit 7281018
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
42 changes: 42 additions & 0 deletions solutions/is_palindrome.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
Created on 04/01/2025
@author: Tibyan Khalid
"""


def is_palindrome(string):
"""is_palindrome fuction will return whether the entry given is a palindrome or not.
Palindrome: a word whose reverse is the same as the original word.
Parameters:
string(str): the string to be checked.
Returns:
string (str): "Palindrome" if the word is a palindrome, otherwise "Not Palindrome".
Raises:
AssertionError: If the argument is not a string or if it's too long.
>>> is_palindrome("RADAR")
'Palindrome'
>>> is_palindrome("radar")
'Palindrome'
>>> is_palindrome("Radar")
'Not Palindrome'
>>> is_palindrome("hello")
'Not Palindrome'
"""
# Defensive assertions
assert isinstance(string, str), "Argument(Input) must be a string"
assert len(string) <= 100, (
"Argument (Input) is too long, max allowed length is 100 characters"
)

if string == string[::-1]:
return "Palindrome"
else:
return "Not Palindrome"
43 changes: 43 additions & 0 deletions solutions/tests/test_is_palindrome.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""
Created on 04/01/2025
@author: Tibyan Khalid
"""

import unittest

from ..is_palindrome import is_palindrome


class TestIsPalindrome(unittest.TestCase):
"""Unittests for the is_palindrome function"""

def test_palindrome(self):
self.assertEqual(is_palindrome("level"), "Palindrome")

def test_not_palindrome(self):
self.assertEqual(is_palindrome("world"), "Not Palindrome")

# Boundary Cases Tests
def test_empty_string(self):
"Empty string is considered a Palindrome"
self.assertEqual(is_palindrome(""), "Palindrome")

def test_single_character(self):
"Any single character is a palindrome as it reads the same backward"
self.assertEqual(is_palindrome("t"), "Palindrome")

def test_non_string_entry(self):
"Raise an error if entry is not a string"
with self.assertRaises(AssertionError):
is_palindrome(34)

def test_upper_lower_cases(self):
"Case matters here, radar is a Palindrome but Radar is not"
self.assertEqual(is_palindrome("Radar"), "Not Palindrome")
self.assertEqual(is_palindrome("Radar".lower()), "Palindrome")

def test_special_characters(self):
"Handles special characters correctly"
self.assertEqual(is_palindrome("t@a@t"), "Palindrome")
self.assertEqual(is_palindrome("a@b@c"), "Not Palindrome")

0 comments on commit 7281018

Please sign in to comment.