Skip to content

Commit fe4302a

Browse files
committed
Create a separate file to store generic functions
1 parent 734fa76 commit fe4302a

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

palindrome/palindrome.py

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,9 @@
1-
def remove_special_char(string):
2-
''' Removes all special characters from a string '''
3-
forbidden_char = (".", ",", "!", ";", ":", "-", "_", "?", "/")
4-
new_string = ""
5-
for s in string:
6-
if s not in forbidden_char:
7-
new_string += s
8-
return new_string
9-
10-
11-
def reverse(string):
12-
''' Reverses a string'''
13-
return string[::-1]
14-
15-
16-
def palindrome_test(word):
17-
''' Returns true if a word is palindrome '''
18-
return remove_special_char(word.lower()) == \
19-
reverse(remove_special_char(word.lower()))
1+
from string_utils import is_palindrome
202

213

224
def main():
235
word = input("Type a word to test if it is palindrome: ")
24-
is_palindrome = palindrome_test(word)
25-
if is_palindrome:
6+
if is_palindrome(word):
267
print(f"The word '{word}' is palindrome")
278
else:
289
print(f"The word '{word}' isn't palindrome")

palindrome/string_utils.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
def remove_special_char(string):
2+
''' Removes all special characters from a string '''
3+
forbidden_char = (".", ",", "!", ";", ":", "-", "_", "?", "/")
4+
new_string = ""
5+
for s in string:
6+
if s not in forbidden_char:
7+
new_string += s
8+
return new_string
9+
10+
11+
def reverse(string):
12+
''' Reverses a string'''
13+
return string[::-1]
14+
15+
16+
def is_palindrome(word):
17+
''' Returns true if a word is palindrome '''
18+
return remove_special_char(word.lower()) == \
19+
reverse(remove_special_char(word.lower()))

0 commit comments

Comments
 (0)