Created
July 12, 2020 10:27
-
-
Save winarcooo/6c0f756da941b8be3b9a463396fa90a8 to your computer and use it in GitHub Desktop.
[balance brackets] #hackerrank #python
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
#!/bin/python3 | |
import math | |
import os | |
import random | |
import re | |
import sys | |
def isPair(open, close): | |
if open == "(" and close == ")": | |
return True | |
elif open == "{" and close == "}": | |
return True | |
elif open == "[" and close == "]": | |
return True | |
return False | |
def isOpen(param): | |
if param in ("(", "{", "["): | |
return True | |
return False | |
def isClose(param): | |
if param in (")", "}", "]"): | |
return True | |
return False | |
# Complete the isBalanced function below. | |
def isBalanced(string): | |
temporary = [] | |
brackets = list(string) | |
for item in brackets: | |
print(isClose(item)) | |
if len(temporary) == 0 and isClose(item): | |
return "NO" | |
if isOpen(item): | |
temporary.append(item) | |
if isClose(item): | |
prevBracket = temporary[-1] | |
if isPair(prevBracket, item): | |
temporary.pop() | |
else: | |
return "NO" | |
if len(temporary) == 0: | |
return "YES" | |
return "NO" | |
if __name__ == '__main__': | |
input0 = "{(([])[])[]]}" | |
input1 = "{[()]}" | |
input2 = "[])" | |
result = isBalanced(input2) | |
print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment