Skip to content

Commit 40ae8c1

Browse files
authored
Merge pull request larymak#89 from aldotele/add_recursive_palindrome
[FEAT]: recursive palindrome challenge
2 parents c73aef8 + 685a766 commit 40ae8c1

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

RecursivePalindrome/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
### Is it a Palindrome?
2+
A word (or sentence) is a palindrome when reading it from right to left is the same as from left to right.
3+
For example: **level** is a palindrome.
4+
5+
The code uses recursion in order to iteratively compare the first and last element of the string,
6+
gradually reducing the string until the middle of it.
7+
Recursion is used when you do not know in advance how many iterations you need in order to get a solution.

RecursivePalindrome/main.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
def isPalindrome(s):
2+
s = s.lower().replace(' ', '') # this allows working on sentences as well
3+
if s == '' or len(s) == 1:
4+
return True
5+
else:
6+
if s[0] != s[-1]:
7+
return False
8+
s = s[1:-1]
9+
return isPalindrome(s) # note: we might delete s = s[1:-1] and write directly return isPalindrome(s[1:-1])
10+
11+
12+
def main():
13+
word = input('enter a string: ')
14+
print('is that a palindrome?')
15+
if isPalindrome(word):
16+
print("Yep")
17+
else:
18+
print("Nope")
19+
20+
21+
if __name__ == '__main__':
22+
main()

0 commit comments

Comments
 (0)