QUESTION 1. A palindrome is a word, phrase, number, or other sequence of symbols
ID: 3697372 • Letter: Q
Question
QUESTION 1.
A palindrome is a word, phrase, number, or other sequence of symbols or elements, whose meaning may be interpreted the same way in either forward or reverse direction (see Wikipedia).
Write a Python program called ‘palindrome.py’ that uses a recursive function to calculate whether or not a string is a palindrome (reads the same if reversed). You may NOT use iteration, string slicing or any other technique to reverse the string without using recursion! Please use Python version 3.5.
Sample Input/Output:
Enter a string:
able was I ere I saw elba
Palindrome!
Sample Input/Output:
Enter a string:
elba is a noob
Not a palindrome!
QUESTION 2.
Write a Python program called ‘palindromeprimes.py’ that uses recursive functions to find all palindromic primes between two integers N, M, supplied as input. (start and end points are included). Please use Python version 3.5.
- A palindrome number is a number that reads the same from the front and the back. Examples are: 212, 44, 9009, 4567654.
- To calculate whether a number is a palindrome or not, incorporate your answer to question 1.
- A prime number is a number greater than 1 that is only divisible by 1 and itself. Examples are: 3, 11, 313.
Some examples of palindromic primes are: 11, 191, 313.
You may assume it’s always the case that N>1, and that NM.
You MUST NOT use any form of loop in your program!
Add the following lines at the top of your program to increase the amount of recursion that Python will allow:
import sys
sys.setrecursionlimit (30000)
Sample Input/Output:
Enter the starting point N:
200
Enter the ending point M:
800
The palindromic primes are:
313
353
373
383
727
757
787
797
Explanation / Answer
Any queries please comment
1)
def is_palindrome(string_in):
if (string_in):
if (string_in[0] == string_in[-1]):
result = is_palindrome(string_in[1:-1])
print string_in, "is palindrome"
if result:
return True
else:
return True ## no more letters
return False
if __name__ == "__main__":
s = raw_input("Enter palindrome string ")
result = is_palindrome( s )
print "The final result is", s,
if not result:
print "is NOT a palindrome"
else:
print "is a palindrome"
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.