Skip to content

Commit 11771d0

Browse files
Andrew hung nguyen - is_palindrome() fixes and method naming convention changes (#75)
* Adding a gitignore to avoid adding pyc * Separated string, statistics, and bitwise operations to separate files and created unit tests * is_palindrome and string manipulation test method names modified * is_palindrome and string manipulation test method names modified
1 parent 00f1181 commit 11771d0

3 files changed

Lines changed: 45 additions & 31 deletions

File tree

-16.2 KB
Binary file not shown.

easyPythonpi/methods/stringmainpulation.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ def remove_punctuation(my_str:'str')->'str':
99
for char in my_str:
1010
if char not in punctuations:
1111
no_punct = no_punct + char
12-
#return no_punct
1312
return no_punct
1413

1514
def count_vowels(ip_str:'str')->'dict':
@@ -29,12 +28,23 @@ def count_vowels(ip_str:'str')->'dict':
2928
#return the count dictionary
3029
return count
3130

32-
def ispalindrome(x:'str')->'bool': # To check if the given parameter is palindrome or not
33-
x=str(x) #explicitly convert into string data type so as to iterate through each character
31+
# To check if the given parameter is palindrome or not
32+
def is_palindrome(x:'str')->'bool':
33+
# convert into string data type so as to iterate through each character
34+
x=str(x)
35+
36+
# remove whitespace, lower case letters, and then remove punctuations
37+
x = x.replace(" ", "")
38+
x = x.lower()
39+
x = remove_punctuation(x)
40+
41+
# Convert original string to its reversed version
3442
r=''
3543
for i in range(len(x)-1,-1,-1):
36-
r=r+x[i]
37-
if x==r: # if the parameter get matched with its reverse then returns true othewise false
38-
return True
44+
r=r+x[i]
45+
46+
# if the parameter get matched with its reverse then returns true othewise false
47+
if x==r:
48+
return True
3949
else:
40-
return False
50+
return False

easyPythonpi/test/test_stringmanipulation.py

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,66 +3,70 @@
33
# Import the function you want to test
44
from methods.stringmainpulation import *
55

6-
class TestCountVowels(unittest.TestCase):
7-
def test_no_vowels(self):
6+
class TestStringManipulation(unittest.TestCase):
7+
def test_count_vowels_string_no_vowels(self):
88
result = count_vowels("rhythm")
99
self.assertEqual(result, {'a': 0, 'e': 0, 'i': 0, 'o': 0, 'u': 0})
1010

11-
def test_all_vowels(self):
11+
def test_count_vowels_string_has_vowels(self):
1212
result = count_vowels("aeiouAEIOU")
1313
self.assertEqual(result, {'a': 2, 'e': 2, 'i': 2, 'o': 2, 'u': 2})
1414

15-
def test_mixed_string(self):
15+
def test_count_vowels_string_vowels_and_consonants(self):
1616
result = count_vowels("Hello, World!")
1717
self.assertEqual(result, {'a': 0, 'e': 1, 'i': 0, 'o': 2, 'u': 0})
1818

19-
def test_empty_string_counting_vowels(self):
19+
def test_count_vowels_empty_string(self):
2020
result = count_vowels("")
2121
self.assertEqual(result, {'a': 0, 'e': 0, 'i': 0, 'o': 0, 'u': 0})
2222

23-
def test_case_insensitive(self):
23+
def test_count_vowels_empty_string(self):
2424
result = count_vowels("Python Is Awesome")
2525
self.assertEqual(result, {'a': 1, 'e': 2, 'i': 1, 'o': 2, 'u': 0})
2626

27-
def test_empty_string(self):
28-
result = ispalindrome("")
27+
def test_count_vowels_string_non_ascii_chars_only(self):
28+
result = count_vowels("#!@$% ?")
29+
self.assertEqual(result, {'a': 0, 'e': 0, 'i': 0, 'o': 0, 'u': 0})
30+
31+
def test_is_palindrome_empty_string(self):
32+
result = is_palindrome("")
2933
self.assertTrue(result)
3034

31-
def test_single_character(self):
32-
result = ispalindrome("a")
35+
def test_is_palindrome_whitespace_only(self):
36+
result = is_palindrome(" ")
3337
self.assertTrue(result)
3438

35-
def test_palindrome_with_spaces(self):
36-
result = ispalindrome("A man a plan a canal Panama")
39+
def test_is_palindrome_single_character(self):
40+
result = is_palindrome("a")
3741
self.assertTrue(result)
3842

39-
def test_palindrome_with_punctuation(self):
40-
result = ispalindrome("Was it a car or a cat I saw?")
43+
def test_is_palindrome_sentence_without_punctuation(self):
44+
result = is_palindrome("A man a plan a canal Panama")
4145
self.assertTrue(result)
4246

43-
def test_non_palindrome(self):
44-
result = ispalindrome("Hello, World!")
47+
def test_is_palindrome_sentence_with_punctuation(self):
48+
result = is_palindrome("Was it a car or a cat I saw?")
49+
self.assertTrue(result)
50+
51+
def test_is_palindrome_sentence_not_palindrome(self):
52+
result = is_palindrome("Hello, World!")
4553
self.assertFalse(result)
4654

47-
def test_empty_string_remove_punctuation(self):
55+
def test_remove_punctuation_empty_string_remove_punctuation(self):
4856
result = remove_punctuation("")
4957
self.assertEqual(result, "")
5058

51-
def test_no_punctuation(self):
59+
def test_remove_punctuation_sentence_no_punctuation(self):
5260
result = remove_punctuation("This is a test string")
5361
self.assertEqual(result, "This is a test string")
5462

55-
def test_with_punctuation(self):
63+
def test_remove_punctuation_sentence_with_punctuation(self):
5664
result = remove_punctuation("Hello, World! This is a test.")
5765
self.assertEqual(result, "Hello World This is a test")
5866

59-
def test_mixed_characters(self):
67+
def test_remove_punctuation_mixed_characters(self):
6068
result = remove_punctuation("123@abc#def%")
6169
self.assertEqual(result, "123abcdef")
6270

63-
def test_unicode_punctuation(self):
64-
result = remove_punctuation("Unicode — string: ™ ©")
65-
self.assertEqual(result, "Unicode string ")
66-
6771
if __name__ == '__main__':
6872
unittest.main()

0 commit comments

Comments
 (0)