1
0
Fork 0
mirror of https://github.com/Lyxal/Keta.git synced 2024-08-14 23:53:13 +00:00

Update chainify.py

This commit is contained in:
JonoCode9374 2019-12-26 20:48:18 +11:00 committed by GitHub
parent a7c9589026
commit c90ee6ca82
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,7 +2,7 @@ dyads = "*+-/%"
monads = list(map(chr, range(ord("A"), ord("Z") + 1)))
nilads = "1234567890"
source = input()
source = input()[::-1]
arities = []
for char in source:
if char in dyads:
@ -15,20 +15,22 @@ for char in source:
arities.append((0, char))
exprs = []
expr = []
patterns = ["02", "10", "11", "12", "20", "21", "22"]
pattern = ""
while len(arities):
# Try grouping triplets
if pattern in patterns:
exprs += [pattern, expr]
expr = []
pattern = ""
pattern += str(arities[-1][0])
expr.append(arities[-1][1])
arities.pop()
if len(arities) - 3 >= 0:
exprs.append(arities[-3:])
del arities[-3:]
# Now, try grouping pair
elif len(arities) - 2 >= 0:
exprs.append(arities[-2:])
del arities[-2:]
else:
exprs.append(arities[-1])
del arities[-1]
if not exprs and pattern in patterns:
exprs += [pattern, expr]
expr = []
pattern = ""
print(exprs)