Python Day 18: Queues and Stacks

Write the following declarations and implementations:

Two instance variables: one for your stack, and one for your queue.
A void pushCharacter(char ch) method that pushes a character onto a stack.
A void enqueueCharacter(char ch) method that enqueues a character in the queue instance variable.
A char popCharacter() method that pops and returns the character at the top of the stack instance variable.

  1. import sys
  2. class Solution:
  3. def __init__(self):
  4. self.mystack = list()
  5. self.myqueue = list()
  6. return(None)
  7. def pushCharacter(self, char):
  8. self.mystack.append(char)
  9. def popCharacter(self):
  10. return(self.mystack.pop(-1))
  11. def enqueueCharacter(self, char):
  12. self.myqueue.append(char)
  13. def dequeueCharacter(self):
  14. return(self.myqueue.pop(0))
  15. # read the string s
  16. s=input()
  17. #Create the Solution class object
  18. obj=Solution()
  19. l=len(s)
  20. # push/enqueue all the characters of string s to stack
  21. for i in range(l):
  22. obj.pushCharacter(s[i])
  23. obj.enqueueCharacter(s[i])
  24. isPalindrome=True
  25. '''
  26. pop the top character from stack
  27. dequeue the first character from queue
  28. compare both the characters
  29. '''
  30. for i in range(l // 2):
  31. if obj.popCharacter()!=obj.dequeueCharacter():
  32. isPalindrome=False
  33. break
  34. #finally print whether string s is palindrome or not.
  35. if isPalindrome:
  36. print("The word, "+s+", is a palindrome.")
  37. else:
  38. print("The word, "+s+", is not a palindrome.")
Please click on the like button if it worked

Solution not working or have any suggestions? Please send an email to [email protected]

donate a cup of tea :)

Join Our Facebook Group

Share this solution is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube