In this activity, you will model a Stack as a stack of index cards.
Start by creating 3 index cards with the following characters:
- "{"
- "["
- "("
As the examples get more complex, you will need to create more index cards when you have multiple of the same character.
Run the check_it.rb
file. For each of the examples provided at the end of the file:
- Every time the
Stack#push
method is called, put that character on top of the Stack. - Every time the
Stack#pop
method is called, remove the top index card from the Stack.
After you have looked at all examples, answer the following questions:
- What is the
check_it
algorithm checking? - What other applications could this algorithm have?
- What kinds of problems are Stacks good at?
Implement the following methods for the Stack
class (be sure to use TDD):
search(element)
- search forelement
and return its index, or nil if not foundpeek
- return, but don't remove, the top elementclear
- clear the Stackempty?
- return a boolean indicating if there are any elements in the stack