30 Day code challenge-python

Ganesh Uthiravasagam
2 min readMar 25, 2021

--

Day 18 — Queues and Stacks

Task:

To solve this challenge, we must first take each character in, enqueue it in a queue, and also push that same character onto a stack. Once that’s done, we must dequeue the first character from the queue and pop the top character off the stack, then compare the two characters to see if they are the same; as long as the characters match, we continue dequeueing, popping, and comparing each character until our containers are empty (a non-match means isn’t a palindrome).

Write the following declarations and implementations:

  1. Two instance variables: one for your stack and queue one for your .
  2. A void pushCharacter(char ch) method that pushes a character onto a stack.
  3. A void enqueueCharacter(char ch) method that enqueues a character in the queue instance variable.
  4. A char popCharacter() method that pops and returns the character at the top of the stack instance variable.
  5. A char dequeueCharacter() method that dequeues and returns the first character in the queue instance variable.
Sample input and output

Input Format

The locked stub code in your editor reads a single line containing string S. It then calls the methods specified above to pass each character to your instance variables.

Output Format

If your code is correctly written and is a palindrome, the locked stub code will print “The word, s, is a palindrome.”; otherwise, it will print “The word, s, is not a palindrome.”

Solution

Explanation

Line 22–30: Gets input from the user and calls the class function of stack and queue.

Line 8–18: Adds and removes value from stack and queue

Line 38–45: Checks whether popCharacter == dequeueCharacter. If yes, then “The word, s, is a palindrome.” is printed or else “The word, s, is not a palindrome.”

Bonus Tip: Instead of “#” for comments we can also use triple single quotes.

See you on day 19

--

--

No responses yet