Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Palindrome using Stacks

$
0
0
One of my assignments was to make a palindrome program, which I did below...


#stack.py

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

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

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

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

    def peek(self):
        return self.items[len(self.items)-1]

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



from stackDS import Stack

def isPalindrome(word):

    if len(word) < 2: return True
    if word[0] != word[-1]: return False
    return isPalindrome(word[1:-1])
    

    return True
    

#Test isPalindrome
print (isPalindrome("maam")) #Expected Output: TRUE
print (isPalindrome("madam")) #Expected Output: TRUE
print (isPalindrome("hello")) #Expected Output: FALSE
print (isPalindrome("macdam")) #Expected Output: FALSE
print (isPalindrome("buffalolaffub")) #Expected Output: TRUE
print (isPalindrome("argentina")) #Expected Output: FALSE



Now she wants the program to do the same thing but using STACKS and no reversing. Can anybody help me with this?

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>