Skip to content

Instantly share code, notes, and snippets.

@yanil3500
Created March 9, 2019 19:41
Show Gist options
  • Save yanil3500/61e32b910bee171700f6312972d824c3 to your computer and use it in GitHub Desktop.
Save yanil3500/61e32b910bee171700f6312972d824c3 to your computer and use it in GitHub Desktop.
Proper Parenthetics
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