Skip to content

Instantly share code, notes, and snippets.

@tparveen
Forked from Rosuav/Stack and Queue exercises.md
Last active December 15, 2017 19:06
Show Gist options
  • Save tparveen/556fd8789d45cc0c67b46fcdf1ca03de to your computer and use it in GitHub Desktop.
Save tparveen/556fd8789d45cc0c67b46fcdf1ca03de to your computer and use it in GitHub Desktop.

Exercises

Palindromes

A palindrome is a word, phrase, or number that is spelled the same forward and backward. For example, “dad” is a palindrome; “A man, a plan, a canal: Panama” is a palindrome if you take out the spaces and ignore the punctuation; and 1,001 is a numeric palindrome. We can use a stack to determine whether or not a given string is a palindrome.

Write a function that takes a string of letters and returns true or false to determine whether it is palindromic. For example:

function is_palindrome(s) {
    s = s.toLowerCase().replace(/[^a-zA-Z0-9]/g, "");
    // your code goes here
}

// true, true, true
console.log(is_palindrome("dad"));
console.log(is_palindrome("A man, a plan, a canal: Panama"));
console.log(is_palindrome("1001"));

Matching parentheses in an expression

A stack can be used to ensure that an arithmetic expression has balanced parentheses. Write a function that takes an arithmetic expression as an argument and returns the position in the expression where a parenthesis is missing or incorrect.

For version 1, the parentheses you need to consider are ( and ). Finding a close parenthesis without an open parenthesis is an error (report the location of the close); reaching the end of the sring while still "holding" an open parenthesis is also an error (report the location of the open).

Extension exercise: Recognize three pairs of brackets: (), [], and {}. These must be correctly nested; "([)]" is incorrect, and should report an error at the ), stating that you were expecting a ] but found a ). If this is starting to look and sound very familiar, congratulations - you're beginning to write a simple language parser!

Extension extension exercise: Also recognize two types of quote character: "" and ''. Inside quotes, brackets aren't counted at all - in fact, nothing is counted until you reach the corresponding close quote.

Square dance pairing

As people come to the dance floor, they should be paired off as quickly as possible: man with woman, man with woman, all the way down the line. If several men arrive in a row, they should be paired in the order they came, and likewise if several women do. Maintain a queue of "spares" (men for whom you have no women yet, or vice versa), and pair them as appropriate.

  • F Jane

  • M Frank

  • M John

  • M Sherlock

  • F Madonna

  • M David

  • M Christopher

  • F Beyonce

    • Female dancer is: Jane and the male dancer is: Frank
    • Female dancer is: Modonna and the male dancer is: John
    • Female dancer is: Beyonce and the male dancer is: Sherlock

There are 2 male dancers waiting to dance

The Ophidian Bank

At the Ophidian Bank, a single teller serves a long queue of people. New customers join the end of the queue, and the teller will serve a customer only if s/he has all the appropriate paperwork. Write a representation of this queue; 25% of the time (random), a customer's paperwork isn't quite right, and it's back to the end of the queue. Show what a few minutes of the bank's lobby would look like.

Optional Exercises

If you are done with the above exercises, here are a few extra exercises for you to practice more in stack and queue.

Sort Stack

Write a program to sort a stack such that the smallest items are on the top. You can use an additional temporary stack, but you may not copy the elements into any other data structure (such as an array).

Queue Implementation using Stack

A common way to implement a queue is to use a doubly linked list. Using the concept of queue in mind, implement a queue using 2 stacks and no other data structure. (You are not allowed to use a doubly linked list or array. Use the stack implementation with Linked list from your today’s reading material)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment