Ad function description
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2021-12-18 15:36:15 +01:00
parent 3de570f27f
commit f137feefe4

View File

@@ -3,6 +3,15 @@ from pathlib import Path
def parse(line): def parse(line):
"""This function parses an input line to nested tuples of length 2. It works by stripping the
outermost bracket-pair from the string and counting the 'depth' of the current brackets. After
we got back to the initial level 0 the next comma marks the index where the left and right part
are seperated. Those will be parsed by a recursive parse call each. If there is no comma on a
line, we are on the deepest level of this branch and return the value as integer. Else the left
and right return value will be returned in a tuple. The result will bea binary tree of the
snailfish number.
Alternatives for parsing would be eval() - which can be unsafe and is slow - or json.parse().
"""
if ',' not in line: if ',' not in line:
return int(line) return int(line)
line = line[1:-1] line = line[1:-1]