Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this part of the assignment, you will write a function (isPalindrome) that u

ID: 3776016 • Letter: F

Question

For this part of the assignment, you will write a function (isPalindrome) that uses a stack to check if a string is a palindrome. The function takes just one argument, the string to check. The function returns True if the string is a palindrome, False otherwise. The basic algorithm will work like this: let n be the length of the string for each of the first n/2 characters in the string push the character onto the stack if n is odd, skip the character at n/2+1 (this is in the middle) for each character on the stack pop the character off the stack compare each character popped to the next character in the string if any do not match, the string is not a palindrome

Explanation / Answer

def isPalindrome(s):
   l = len(s)
   mid = l/2
   li=[]
   for i in range(0,mid):
       li.append(s[i])
   if(l%2==0):
       hat = mid
   else:
       hat = mid+1
   for i in range(hat,l):
       if(li[-1]==s[i]):
           li = li[0:-1]
       else:
           return False
   if(len(li)==0):
       return True
   return False
print(isPalindrome("soos"))
print(isPalindrome("baab"))
print(isPalindrome("baa"))
print(isPalindrome("mike"))

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote