Write two functions (isPalindrome() and getLongestPalindromicSubstring()) The fi
ID: 3580094 • Letter: W
Question
Write two functions (isPalindrome() and getLongestPalindromicSubstring()) The first function (isPalindrome()) takes a single string argument (string), and returns True if the string is a palindrome (a string spelled the same backward and forward), and False otherwise The function must use recursion, and not loops to perform this task. The second function (getLongestPalindromicSubstring()) takes a single string argument (string), and returns the longest substring of string that is a palindrome This function must call the isPalindrome() function in order to check H a substring is a palindrome Sample usage: > > > isPalindrome 'level') True > > >isPalindrome ('level') false > > > getLongestPalindromeSubstring (' banana') anana > > > getLongestPalindromeSubstring ('abracadabra') acaExplanation / Answer
def isPalindrome(s):
ln = len(s)
i=0
j=ln-1
hat=0
while(i<j):
if(s[i]!=s[j]):
hat=1
break
i+=1
j-=1
if(hat==1):
return False
return True
def getLongestPalindromicSubstring(s):
tmp=0
mx=0
ans=""
ln = len(s)
for i in range(0,ln):
for j in range(i+1,ln+1):
if(isPalindrome(s[i:j])):
tmp = j-i
if(tmp>mx):
mx=tmp
ans=s[i:j]
return ans
d=isPalindrome("aba")
print(d)
d=isPalindrome("nama")
print(d)
d=getLongestPalindromicSubstring("banana")
print(d)
d=getLongestPalindromicSubstring("abracadabra")
print(d)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.