Created
February 11, 2011 22:03
Prints almost matched parens
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
#!/usr/bin/python | |
from random import random | |
BE_EVIL = True | |
MAX_DEPTH = 20 | |
PROB_t = 0.05 | |
PROB_p = 0.4 | |
def production(depth): | |
depth += 1 | |
if depth > MAX_DEPTH or random() < PROB_t: | |
return terminal() | |
elif random() < PROB_p: | |
return '%s%s' % (production(depth), | |
production(depth)) | |
else: | |
return '(%s)' % production(depth) | |
def terminal(): | |
global BE_EVIL | |
if BE_EVIL: | |
BE_EVIL = False | |
return '(' | |
else: | |
return '()' | |
print '(%s)' % production(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why are you printing almost matched parens?