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:
parent
a7c9589026
commit
c90ee6ca82
1 changed files with 16 additions and 14 deletions
28
chainify.py
28
chainify.py
|
@ -2,7 +2,7 @@ dyads = "*+-/%"
|
||||||
monads = list(map(chr, range(ord("A"), ord("Z") + 1)))
|
monads = list(map(chr, range(ord("A"), ord("Z") + 1)))
|
||||||
nilads = "1234567890"
|
nilads = "1234567890"
|
||||||
|
|
||||||
source = input()
|
source = input()[::-1]
|
||||||
arities = []
|
arities = []
|
||||||
for char in source:
|
for char in source:
|
||||||
if char in dyads:
|
if char in dyads:
|
||||||
|
@ -15,20 +15,22 @@ for char in source:
|
||||||
arities.append((0, char))
|
arities.append((0, char))
|
||||||
|
|
||||||
exprs = []
|
exprs = []
|
||||||
|
expr = []
|
||||||
|
patterns = ["02", "10", "11", "12", "20", "21", "22"]
|
||||||
|
pattern = ""
|
||||||
while len(arities):
|
while len(arities):
|
||||||
# Try grouping triplets
|
if pattern in patterns:
|
||||||
|
exprs += [pattern, expr]
|
||||||
|
expr = []
|
||||||
|
pattern = ""
|
||||||
|
|
||||||
if len(arities) - 3 >= 0:
|
pattern += str(arities[-1][0])
|
||||||
exprs.append(arities[-3:])
|
expr.append(arities[-1][1])
|
||||||
del arities[-3:]
|
arities.pop()
|
||||||
|
|
||||||
# Now, try grouping pair
|
if not exprs and pattern in patterns:
|
||||||
elif len(arities) - 2 >= 0:
|
exprs += [pattern, expr]
|
||||||
exprs.append(arities[-2:])
|
expr = []
|
||||||
del arities[-2:]
|
pattern = ""
|
||||||
else:
|
|
||||||
exprs.append(arities[-1])
|
|
||||||
del arities[-1]
|
|
||||||
|
|
||||||
print(exprs)
|
print(exprs)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue