Last active
April 9, 2021 11:37
-
-
Save nrubin29/f9bd37efe4265679285b9e2ef46f312a to your computer and use it in GitHub Desktop.
The classic balanced parentheses algorithm implemented in one line (one expression) in Python for this video: https://youtu.be/0eeweCiUU4U
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
print( | |
( | |
lambda expr, stack: all( | |
len(stack) == 0 if char is None else | |
stack.append(char) or True if char in '([{' else | |
len(stack) > 0 and {'(': ')', '[': ']', '{': '}'}[stack.pop()] == char if char in ')]}' else | |
True | |
for char in expr | |
) | |
)( | |
list(input()) + [None], [] | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment