Using this stack:
class myStack:
def __init__(self):
self.container = []
def __repr__(self):
return "Stack({})".format(self.container)
def push(self,item):
self.container.append(item)
def __iter__(self):
return reversed(self.container)
def pop(self):
return self.container.pop()
def len(self):
return len(self.container)
Could you write a client function called parenthesesMatch? 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.
This is my algorithm: 1. Create an empty stack. 2. Iterate over the characters in the given string: a. If the character is one of opening marks(,[,{ push it on the stack. b. 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. c. 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. 3. Once the iteration is finished, you know that the parentheses match if and only if the stack is empty.