-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalindrome.py
45 lines (32 loc) · 911 Bytes
/
palindrome.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import re
from string import punctuation
def create_pattern():
chars = punctuation
chars = list(map(lambda x: re.escape(x), chars))
chars.append("\s")
return "|".join(chars)
def palindrome(phrase):
pattern = create_pattern()
phrase = re.sub(pattern, "", phrase)
# Set phrase in lowercase
phrase = phrase.lower()
# Conditional Validate
is_palindrome = phrase == phrase[::-1]
message = {
True: f"{phrase} is Palindrome",
False: f"{phrase} isn't Palindrome"
}.get(is_palindrome)
print(message)
if __name__ == "__main__":
divisor = "*" * 50
menu = (
divisor,
"Palindrome Checker".center(50),
divisor,
"Add a phrase, word or number, it will remove",
"special chars and spaces.",
divisor
)
print(*menu, sep="\n")
data = input("Validate: ")
palindrome(data)