Question
Using Python 3+ A stack is a sequence container type that, like a queue, supports very restrictive access methods: all insertions and removals are from
Using Python 3+
A stack is a sequence container type that, like a queue, supports very restrictive access methods: all insertions and removals are from one end of the stack, typically referred to as the top of the stack. A stack is often referred to as a list-in first-out (LIFO) container because the last item inserted is the first removed. Implement a Stack class. It should support the following methods/functions:
_init__ - Can construct either an empty stack, or initialized with a list of items, the first item is at the bottom, the last is at the top.
push() take an item as input and push it on the top of the stack
pop() remove and return the item at the top of the stack
isEmpty() returns True if the stack is empty, False otherwise
[] return the item at a given location, [0] is at the bottom of the stack
len() return length of the stack
The object is to make this client code work:
##### Stack #####
>>> s = Stack() >>> s.push('apple') >>> s Stack(['apple']) >>> s.push('pear') >>> s.push('kiwi') >>> s Stack(['apple', 'pear', 'kiwi']) >>> top = s.pop() >>> top 'kiwi' >>> s Stack(['apple', 'pear']) >>> len(s) 2 >>> s.isEmpty() False >>> s.pop() 'pear' >>> s.pop() 'apple' >>> s.isEmpty() True >>> s = Stack(['apple', 'pear', 'kiwi']) >>> s = Stack(['apple', 'pear', 'kiwi']) >>> s[0] 'apple' >>>
'''check that Stacks constructed without a list remain distinct if you receive an error on s2 then you probably need to use a default argument of None in __init__ (see the Queue class developed in class for an example)'''
>>> s = Stack() >>> s.push('apple') >>> s Stack(['apple']) >>> s2 = Stack() >>> s2.push('pear') >>> s2 # if this fails - see the TEST file for explanation Stack(['pear'])
Write a client function parenthesesMatch that given a string containing only the characters for parentheses, braces or curly braces, i.e., the characters in ([{}]), returns True if the parentheses, brackets and braces match and False otherwise. Your solution must use a Stack. For, example:
The object is to make this client code work:
##### parenthesesMatch #####
>>> parenthesesMatch('(){}[]') True >>> parenthesesMatch('{[()]}') True >>> parenthesesMatch('((())){[()]}') True >>> parenthesesMatch('(}') False >>> parenthesesMatch(')(][') # right number, but out of order False >>> parenthesesMatch('([)]') # right number, but out of order False >>> parenthesesMatch('({])') False >>> parenthesesMatch('((())') False >>> parenthesesMatch('(()))') False
>>> Hint: It is not sufficient to just count the number of opening and closing marks. But, it is easy to write this as a simple application of the Stack class. Here is an algorithm:
Create an empty stack.
Iterate over the characters in the given string:
If the character is one of opening marks(,[,{ push it on the stack.
If the character is one of the closing marks ),],} and the stack is empty, then there were not enough preceding opening marks, so return False.
If the character is a closing mark and the stack is not empty, pop an (opening) mark from the stack. If they are not of the same type, ie., ( and ) or [ and ] or { and }, return False, if they are of the same type, move on to the next char.
Once the iteration is finished, you know that the parentheses match if and only if the stack is empty.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started