Created
March 9, 2019 19:41
-
-
Save yanil3500/61e32b910bee171700f6312972d824c3 to your computer and use it in GitHub Desktop.
Proper Parenthetics
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 is_valid(string): | |
if (len(string)) % 2 != 0: | |
return False | |
braces = {'[':']', '{': '}', '(': ')'} | |
opening_braces = braces.keys() | |
stack = [] | |
for char in string: | |
if char in opening_braces: | |
stack.append(char) | |
else: | |
is_stack_empty = len(stack) == 0 | |
if is_stack_empty or braces.get(stack.pop(), None) != char: | |
return False | |
return len(stack) == 0 | |
def main(): | |
result = is_valid('{[({})]}') | |
print(result) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment