Skip to content

Instantly share code, notes, and snippets.

@jeeyoungk
Created June 18, 2011 07:39
Show Gist options
  • Save jeeyoungk/1032899 to your computer and use it in GitHub Desktop.
Save jeeyoungk/1032899 to your computer and use it in GitHub Desktop.
Convoluted way to check for balanced parenthesis.
# Author : Jeeyoung Kim
# Convoluted way to check for balanced parenthesis.
L = lambda f: lambda x : f(x + 1)
R = lambda f: lambda x : not x or f(x - 1)
D = {'(':'L', ')':'R'}
while True:
print 'please input sequence of parenthesises, like ()()((, or ((())())'
input = raw_input()
if not input: break
exec ('\n'.join('@' + D[x] for x in input) + '\ndef stub(x): return bool(x)\nresult=not stub(0)')
print 'Balanced' if result else 'Not balanced.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment