Question
Create a file lab3.py and import your Stack class to implement the following exercise. Test your implementation of this exercise Listing 3.4 (Validation of General
Create a file lab3.py and import your Stack class to implement the following exercise. Test your implementation of this exercise
Listing 3.4 (Validation of General Parentheses-expressions)
from pythonds.basic import Stack def parChecker(symbolString): s = Stack()
balanced = True index = 0
while index < len(symbolString) and balanced: symbol = symbolString[index] if symbol in "([{": s.push(symbol) else: if s.isEmpty(): balanced = False else: top = s.pop() if not matches(top, symbol): balanced = False
index = index + 1
if balanced and s.isEmpty(): return True else: return False
def matches(open, close): opens = "([{" closers = ")]}"
return opens.index(open) == closers.index(close)
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