forked from tiyd-python-2015-01/palindrome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalindrome_recursive.py
25 lines (22 loc) · 1.11 KB
/
palindrome_recursive.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
import re
def palindrome(words, counter, n_counter):
"""Will return if a word is a palindrome, through a recursive method."""
if words[counter] != words[n_counter]:
print('{} is not {}.'.format(words[counter], words[n_counter]))
print('{} is not a palindrome'.format(words))
elif counter == (len(words) - 1):
print('{} is the same as {}.'.format(words[counter], words[n_counter]))
print('{} is a palindrome'.format(words))
else:
print('{} is the same as {}.'.format(words[counter], words[n_counter]))
counter += 1
n_counter -= 1
return palindrome(words, counter, n_counter)
words = input('Please enter a word: ')
words = re.sub(r'[^A-Za-z]', '', words)
print('The program will check the first and last character of the input.')
print('If they are the same it will increment a positive and negative counter.')
print('Then it recalls the function and checks the next character.')
print('If all characters are the same it will tell you its a palindrome.)
print('Anypoint the characters are not the same it will stop.')
palindrome(words.lower(), 0, -1)