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

A palindrome is a sequence of words that reads the same as the sequence in rever

ID: 3748348 • Letter: A

Question

A palindrome is a sequence of words that reads the same as the sequence in reverse: for example, "noon" is a palindrome whereas "noone" is not. Single character is also palindrome. Write a function, is_palindrome(s), which takes a string, s, as a parameter and determines whether the string is a palindrome or not. Ignore spaces and all punctuation symbols. You can assume that all characters are in lower case. You must make use of a deque to test the parameter string. Note: you will be defining a function that USES the Deque ADT. The implementation of the Deque ADT is provided to you as part of this exercise. You can simply use: Deque(), add_rear), remove front(), remove_rear) and size() as necessary in your function definition For example Test Result print(is_palindrome ("abcdef")) False print (is_palindrome ("no,on")) True

Explanation / Answer

palindrome.py

#palidrome checking using deque

from deque import Deque

def is_palindrome(s):

q = Deque()
flag = True
for char in s:
    q.add_front(char)
print q
for i in range((len(s)/2)-1):
     if   q.remove_front() != q.remove_rear():
       return False
return flag


print is_palindrome("abba")
print is_palindrome("aba")
print is_palindrome("abcd")
print is_palindrome("malayalam")


deque.py

#implementation of deque

class Deque:

def __init__(self):
    self.items = []

def is_empty(self):
    return self.items == []

def add_rear(self,item):
    self.items.insert(0, item)

def add_front(self, item):
    self.items.append(item)

def remove_rear(self):
    return self.items.pop(0)

def remove_front(self):
    return self.items.pop()

def size(self):
    return len(self.items)

def __str__(self):
    return "Deque is "+str(self.items)

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