-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathContinuousPalindromeComparer.py
More file actions
96 lines (83 loc) · 3.91 KB
/
ContinuousPalindromeComparer.py
File metadata and controls
96 lines (83 loc) · 3.91 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import string
#Pre: Prompt exists and user needs to enter a input.
#Post: Prompt is displayed to user in order to get input and input is returned.
def GetNewStringVal(Prompt):
return input(Prompt)
#Pre: StringVal exists and a loop needs to keep displaying if an entered
# StringVal is a palindrome, then ask for another StringVal.
#Post: Until the entered StringVal is "exit", the function loops, assesses
# the StringVal, displays a prompt saying if the StringVal is a palindrome
# or not, and gets a new StringVal from the user.
def ContinuousPalindromeComparer():
StringVal = GetNewStringVal("Enter a string:")
while StringVal != "exit":
Prep_StringVal = PrepPalindromeStr(StringVal)
if IsStringPalindrome(Prep_StringVal) == True:
DisplayPrompt("This is a palindrome.")
StringVal = GetNewStringVal("Enter a string:")
else:
DisplayPrompt("This is NOT a palindrome.")
StringVal = GetNewStringVal("Enter a string:")
#Pre: String exists and needs to be confirmed or denied a palindrome.
#Post: If String is found to not be a palindrome, False is returned; otherwise,
# True is returned.
def IsStringPalindrome(String):
Position = 0
for character in range(len(String)//2):
if String[0+Position] != String[-1-Position]:
return False
else:
Position = Position + 1
return True
#Pre: String exists and needs to be prepped for assessing if it is a palindrome.
#Post: String is made lowercase and whitespaces and punctuation are removed;
# then NewestStr is returned.
def PrepPalindromeStr(String):
NewStr = String.lower()
NewerStr = RemoveWhiteSpace(NewStr)
NewestStr = RemovePunctuation(NewerStr)
return NewestStr
#Pre: String exists and needs the whitespaces removed from it.
#Post: The whitespaces are removed from String and StrNoSpace is returned.
def RemoveWhiteSpace(String):
WhiteSpace = string.whitespace
StrNoSpace = ""
for character in String:
if character not in WhiteSpace:
StrNoSpace = StrNoSpace + character
return StrNoSpace
#Pre: String exists and needs punctuation removed from it.
#Post: The punctuation is removed from String and StrNoPunct is returned.
def RemovePunctuation(String):
punctuation = string.punctuation
StrNoPunct = ""
for character in String:
if character not in punctuation:
StrNoPunct = StrNoPunct + character
return StrNoPunct
#Pre: Prompt exists and needs to be displayed to the user.
#Post: Prompt is displayed for user to see.
def DisplayPrompt(Prompt):
print(Prompt)
def main():
print("Welcome to the Continuous Palindrome Comparer!")
print("Enter input and press enter to see if it is a palindrome.")
print("Enter 'exit' without quotes to exit the program.")
ContinuousPalindromeComparer()
# Test Cases
# Input Expected Result
# ------------------------------ ---------------
# 1234321 This is a palindrome.
# radar This is a palindrome.
# RadAr This is a palindrome.
# rad ar This is a palindrome.
# Rad Ar. This is a palindrome.
# Racecar This is a palindrome.
# App le This is NOT a palindrome.
# Cat This is NOT a palindrome.
# number. This is NOT a palindrome.
# "CHeeSe!" This is NOT a palindrome.
# Dammit I'm mad. This is a palindrome.
# A man, a plan, a canal, Panama. This is a palindrome.
# "exit" This is NOT a palindrome
# exit *Program ends*