Skip to content

Instantly share code, notes, and snippets.

@kingoflolz
Created September 6, 2015 08:41
Show Gist options
  • Save kingoflolz/3afa2b40419674728e12 to your computer and use it in GitHub Desktop.
Save kingoflolz/3afa2b40419674728e12 to your computer and use it in GitHub Desktop.
from pprint import pprint
import json
def application(functor, arg, slash):
if functor is None or arg is None:
return None
if '/' in arg or '\\' in arg:
arg = '(' + arg + ')'
arg = slash + arg
if functor.endswith(arg):
res = functor[:-len(arg)]
if '/' in res or '\\' in res:
return res[1:-1]
return res
def fwd_app(left, right):
return application(left, right, '/')
def bwd_app(left, right):
return application(right, left, '\\')
levels = [[]]
things = input("Enter categories: ").split(" ")
for t in things:
levels[0].append([t])
j = 0
#pprint(levels)
while j < len(things):
i = 0
levels.append([])
#for t in range(j):
# while i < len(levels[j-1-t])-1:
# if t == 0:
# levels[j].append([])
# for x in levels[j-1-t][i]:
# #print(levels[j][i])
# for y in levels[j-1+t][i+1-t]:
# left, right = x, y
# if fwd_app(left, right) is not None:
# levels[j][-1].append(fwd_app(left, right))
# if bwd_app(left, right) is not None:
# levels[j][-1].append(bwd_app(left, right))
# i += 1
#j += 1
for current_cell in range(len(levels[j-1])-1):
levels[j].append([])
for vertical in range(j):
left = levels[vertical][current_cell]
#print(levels[vertical],vertical)
right = levels[j-vertical-1][current_cell+vertical+1]
for l in left:
for r in right:
if l != "conj":
levels[j][-1].append(bwd_app(l, r))
levels[j][-1].append(fwd_app(l, r))
#levels[j][-1].append("left: " + str(vertical) +" ,"+ str(current_cell)+ " right: " + str(j-vertical-1) +" ,"+ str(current_cell+vertical+1) + "current: " + str(j) +" ,"+ str(len(levels[j])-1))
else:
levels[j][-1].append(r+"\\"+r)
j += 1
chart = levels[::-1][1:]
print(chart)
#pprint(levels)
#print(json.dumps(levels[::-1][1:] ,indent=4, separators=(',', ': ')))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment