I had to model it in Python

before writing it in OCaml.
This commit is contained in:
Simon Forman
2022-09-28 18:21:13 -07:00
parent 4200c6708e
commit cdec813b24
2 changed files with 120 additions and 0 deletions
+22
View File
@@ -0,0 +1,22 @@
def f(string, start=0, acc=[]):
if start >= len(string):
return acc
if '[' == string[start]:
return [1] + f(string, start+1, acc)
if ']' == string[start]:
return [0] + f(string, start+1, acc)
if ' ' == string[start]:
return f(string, start+1, acc)
symbol, n = bar(string, start, start)
return [symbol] + f(string, n, acc)
def bar(string, start, end):
if end >= len(string) or string[end] in '[] ':
return string[start:end], end
return bar(string, start, end+1)
print(f("1[2[] 3]4"))