diff options
author | Igor Pashev <pashev.igor@gmail.com> | 2022-12-17 20:05:18 +0200 |
---|---|---|
committer | Igor Pashev <pashev.igor@gmail.com> | 2022-12-17 20:07:06 +0200 |
commit | 4c61d8648252c0d52c26ba9d3b5df65e524ff6e4 (patch) | |
tree | 17da2ef7b83e7d69e50ccac0a235ff2b36f5625c | |
parent | 82f184423334fad00ae717339de338a122e1e3fb (diff) | |
download | mendeleev-4c61d8648252c0d52c26ba9d3b5df65e524ff6e4.tar.gz |
Python: use trees
-rw-r--r-- | mendeleev.py | 30 |
1 files changed, 14 insertions, 16 deletions
diff --git a/mendeleev.py b/mendeleev.py index 11776f6..f5ab020 100644 --- a/mendeleev.py +++ b/mendeleev.py @@ -70,27 +70,25 @@ def split(tail): return result or [("?", tail[1:])] -def advance(els, tail): - return [(els + [e], t) for (e, t) in split(tail)] +def explode(tail): + return [(x[0], explode(x[1]) if x[1] else None) for x in split(tail)] -def explode(word): - result = [([], word.lower().encode())] - while result[0][1]: - new = [] - for res in result: - if res[1]: - adv = advance(*res) - new.extend(adv) - else: - new.append(res) +def analyze(word): + return explode(word.lower().encode()) - result = new - return [els for els, _ in result] +def print_plain(tree, formula): + for x in tree: + formula.append(x[0]) + if x[1]: + print_plain(x[1], formula) + else: + print(" " + " ".join(formula)) + formula.pop() for w in sys.argv[1:]: print(w + ":") - for f in filter(None, explode(w)): - print(" " + " ".join(f)) + if w: + print_plain(analyze(w), []) |