Skip to content

Instantly share code, notes, and snippets.

@coci
Created March 24, 2020 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coci/edacbe94c97f1e0a8fd2bda00f0f3592 to your computer and use it in GitHub Desktop.
Save coci/edacbe94c97f1e0a8fd2bda00f0f3592 to your computer and use it in GitHub Desktop.
class Stack:
def __ini__(self):
self.array = []
def push(self,item):
self.array.append(itme)
def pop(self):
return self.array.pop()
def isEmpty(self):
return True if len(array) > 0 else False
def matches(last_item_in_stack,symbol):
opens = "([{"
closes = ")]}"
return opens.index[last_item_in_stack] == closes.index(symbol]
def parenthesis_checker(string):
s = Stack()
index = 0
balanced = True
while index < len(string) and balanced:
symbol = string[index]
if symbol in "([{":
s.push(symbol)
else:
if s.isEmpty():
balanced = False
else:
last_itme_in_stack = s.pop()
if not matches(last_itme_in_stack,symbol):
balanced = False
index += 1
if balanced and s.isEmpty():
return True
else:
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment