Created
April 22, 2020 17:37
-
-
Save Waltari10/67152b30849b359a7a9dfd9e1b5449a9 to your computer and use it in GitHub Desktop.
This file contains 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
def isLeft(bracket): | |
if bracket == "(": | |
return True | |
if bracket == "[": | |
return True | |
if bracket == "{": | |
return True | |
return False | |
def isPair(bracket2, bracket1): | |
if bracket1 == '(' and bracket2 == ')': | |
return True | |
if bracket1 == '[' and bracket2 == ']': | |
return True | |
if bracket1 == '{' and bracket2 == '}': | |
return True | |
def solution(a): | |
stack = [] | |
for bracket in a: | |
if len(stack) > 0 and isPair(bracket, stack[len(stack) - 1]): | |
stack.pop() | |
else: | |
stack.append(bracket) | |
if len(stack) == 0: | |
return 1 # correct | |
else: | |
return 0 # wrong | |
if solution("[{}{}]") == 1: | |
print('SUCCESS 1') | |
if solution("{[{}}]") == 0: | |
print('SUCCESS 2') | |
if solution("[()]()]") == 0: | |
print('SUCCESS 3') | |
if solution("[{()}]") == 1: | |
print('SUCCESS 4') | |
if solution("][") == 0: | |
print('SUCCESS 5') | |
if solution("()[]{}()[]{}") == 1: | |
print('SUCCESS 6') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment