Created
March 24, 2020 15:43
-
-
Save coci/edacbe94c97f1e0a8fd2bda00f0f3592 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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