Skip to content

Instantly share code, notes, and snippets.

@nrubin29
Last active April 9, 2021 11:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nrubin29/f9bd37efe4265679285b9e2ef46f312a to your computer and use it in GitHub Desktop.
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
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