Created
January 30, 2012 14:47
-
-
Save nyuichi/1704788 to your computer and use it in GitHub Desktop.
LazyK (incomplete)
This file contains 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
import re, sys | |
def lex(s): | |
for m in re.finditer(r'S|I|K|\(|\)', s): | |
yield m.group(0) | |
def parse(s): | |
l = lex(s) | |
while True: | |
x = next(l) | |
if x == '(': | |
yield parse_chunk(l) | |
else: | |
yield x | |
def parse_chunk(l): | |
x = '(' | |
i = 1 | |
while i: | |
x += next(l) | |
if x[-1] == ')': | |
i -= 1 | |
if x[-1] == '(': | |
i += 1 | |
return x | |
def run(s): | |
a = list(parse(s)) | |
try: | |
if a[0] == 'I': | |
return run(a[1]) | |
elif a[0] == 'K': | |
return run(a[1]) | |
elif a[0] == 'S': | |
x = "({0} {2}) ({1} {2})" | |
return run(x.format(a[1],a[2],a[3])) | |
else: | |
a[0] = a[0][1:-1] | |
return run(''.join(a)) | |
except: | |
return s | |
print(run(input())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment