From 7514e6bb228e58c1e60eaaeb71261b8f1b19e428 Mon Sep 17 00:00:00 2001 From: gamerdonkey Date: Fri, 12 Sep 2025 03:20:47 +0000 Subject: [PATCH] Improving depth-first algorithm --- recipe_json_to_html.py | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/recipe_json_to_html.py b/recipe_json_to_html.py index c218bb9..dfd4faa 100644 --- a/recipe_json_to_html.py +++ b/recipe_json_to_html.py @@ -4,10 +4,13 @@ from anytree.importer import DictImporter from sys import argv -def build_table_depth_first(node, rows, leaf_count=0): +def build_table_depth_first(node, rows=None): + + if rows is None: + rows = [] for child in sorted(node.children, key=lambda x: x.height, reverse=True): - leaf_count = build_table_depth_first(child, rows, leaf_count) + rows = build_table_depth_first(child, rows) colspan = 0 if node.siblings: @@ -17,20 +20,17 @@ def build_table_depth_first(node, rows, leaf_count=0): colspan = max_sibling_height - node.height + 1 if node.is_leaf: - cur_row = leaf_count - rows[cur_row].append(f'{node.name}') + rows.append([f'{node.name}']) if colspan > 1: - rows[cur_row].append(f'') - - leaf_count += 1 + rows[-1].append(f'') else: child_leaves = len(node.leaves) - cur_row = leaf_count - child_leaves + cur_row = len(rows) - child_leaves rows[cur_row].append(f"{node.name}") - return leaf_count + return rows def generate_steps_depth_first(node, steps=[]): @@ -108,12 +108,7 @@ for sub_recipe in sub_recipes: print("

") print("") - output_rows = [] - - for i in range(len(sub_recipe.leaves)): - output_rows.append([]) - - build_table_depth_first(sub_recipe, output_rows) + output_rows = build_table_depth_first(sub_recipe) for row in output_rows: print("")