Using Python and Stacks For this part of the assignment you

Using Python and Stacks:

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

Solution

def main():

    my_str = input(\"Enter a string: \")

    my_str2 = [c for c in my_str.lower() if c.isalpha()]

    rev_str = reversed(my_str2)

    # check if the string is equal to its reverse

    if list(my_str2) == list(rev_str):

        print(my_str,\"is a palindrome\")

    else:

        print(my_str, \"is not a palindrome\")

if __name__ == \'__main__\':

    main()

But they want us to use a stack and a queue so how would i go about doing that? Here are the stack and queue classes

class Stack:

    \"\"\"Top of the stack is at the end of the list\"\"\"

def __init__(self):

    self._items = []

def push(self, obj):

    self._items.append(obj)

def pop(self):

    return self._items.pop()

def peek(self):

    return self._items[-1]

def isEmpty(self):

    return len(self._items) == 0

def __len__(self):

    return len(self._items)

def __str__(self):

    return \"bottom \" + str(self._items) + \" top\"

def reverse(self):

    return self._items.reverse()

stack = Stack()

stack2 = Stack()

class Queue:

    def __init__(self):

        self.items = []

    def isEmpty(self):

        return self.items == []

    def enqueue(self, item):

        self.items.insert(0,item)

    def dequeue(self):

        return self.items.pop()

    def size(self):

        return len(self.items)

Using Python and Stacks: 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 f
Using Python and Stacks: 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 f

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site