2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
28
Task/Palindrome-detection/Python/palindrome-detection-5.py
Normal file
28
Task/Palindrome-detection/Python/palindrome-detection-5.py
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
def p_loop():
|
||||
import re, string
|
||||
re1="" # Beginning of Regex
|
||||
re2="" # End of Regex
|
||||
pal=raw_input("Please Enter a word or phrase: ")
|
||||
pd = pal.replace(' ','')
|
||||
for c in string.punctuation:
|
||||
pd = pd.replace(c,"")
|
||||
if pal == "" :
|
||||
return -1
|
||||
c=len(pd) # Count of chars.
|
||||
loops = (c+1)/2
|
||||
for x in range(loops):
|
||||
re1 = re1 + "(\w)"
|
||||
if (c%2 == 1 and x == 0):
|
||||
continue
|
||||
p = loops - x
|
||||
re2 = re2 + "\\" + str(p)
|
||||
regex= re1+re2+"$" # regex is like "(\w)(\w)(\w)\2\1$"
|
||||
#print(regex) # To test regex before re.search
|
||||
m = re.search(r'^'+regex,pd,re.IGNORECASE)
|
||||
if (m):
|
||||
print("\n "+'"'+pal+'"')
|
||||
print(" is a Palindrome\n")
|
||||
return 1
|
||||
else:
|
||||
print("Nope!")
|
||||
return 0
|
||||
Loading…
Add table
Add a link
Reference in a new issue