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

Implement the function is_paiindrone() in palindrome.py that returns True if the

ID: 3804954 • Letter: I

Question

Implement the function is_paiindrone() in palindrome.py that returns True if the argument s is a palindrome (ie, reads the same forwards and backwards), and False otherwise. You may assume that s is all lower case and doesn't any whitespace characters. $ python palindrome.py bolton False $ python palindrome.py amanaplanacanalpanama True |# Return True if s is a palindrome and False otherwise. You may assume that # s is all lower case and doesn't any whitespace characters. def is_palindrome(s): # Iterate over half of the string s. Compare character at i with # the character at len(s) - i - 1. If they are different, return False # Otherwise, continue. Return True when the loop is exhausted. for i in ...: # Test client [DO NOT EDIT]. Reads a string s as command-line argument and # prints True if s is a palindrome, ie, reads the same forwards and backwards, # and False otherwise, def _main(): s = sys.argv[1] stdio.write In(is_palindrome(s)) if _name_ == '_main_': _main()

Explanation / Answer

def is_palindrome(s):
   l = len(s) #The length of the string
   # Inorder for it to a plaindrome the last character must match
   # with 1st and last-1 character must match with 2nd and so on
   #iterating from start to half of the string
   for i in range(0,l/2):
       if (s[i] != s[l-i-1]):
           return False
   #If the program reached until this point. The all characters match so, it is a palindrome.
   return True