Skip to content

Commit 6141f64

Browse files
committed
feat: improve _build_problem_tags_cache
1 parent 5a3566b commit 6141f64

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

.amazonq/rules/problem-creation.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ When user requests a problem by **number** or **name/slug**, the assistant will:
1313
- Images provide crucial visual context, especially for tree and graph problems
1414
- Always verify images are included in `readme_examples` and accessible
1515
4. **Create** JSON file in `leetcode_py/cli/resources/leetcode/json/problems/{problem_name}.json`
16-
5. **Update** Makefile with `PROBLEM ?= {problem_name}`
17-
6. **Generate** problem structure using `make p-gen`
18-
7. **Verify** with `make p-lint` - fix template issues in JSON if possible, or manually fix generated files if template limitations
19-
8. **Iterate** if JSON fixes: re-run `make p-gen PROBLEM={problem_name} FORCE=1` and `make p-lint` until passes to ensure reproducibility
16+
5. **Update tags.json5** - If user specifies tags, manually add problem name to corresponding tag arrays in `leetcode_py/cli/resources/leetcode/json/tags.json5`
17+
6. **Update** Makefile with `PROBLEM ?= {problem_name}`
18+
7. **Generate** problem structure using `make p-gen`
19+
8. **Verify** with `make p-lint` - fix template issues in JSON if possible, or manually fix generated files if template limitations
20+
9. **Iterate** if JSON fixes: re-run `make p-gen PROBLEM={problem_name} FORCE=1` and `make p-lint` until passes to ensure reproducibility
2021

2122
## Scraping Commands
2223

leetcode_py/cli/utils/problem_finder.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,23 @@ def _add_problem_to_tag_map(
5959
problem_tags_map[problem_name].append(tag_name)
6060

6161

62+
def _process_tag_reference(
63+
tags_data: dict, item: dict, tag_name: str, problem_tags_map: dict[str, list[str]]
64+
) -> None:
65+
for problem_name in tags_data.get(item["tag"], []):
66+
if isinstance(problem_name, str):
67+
_add_problem_to_tag_map(problem_tags_map, problem_name, tag_name)
68+
69+
70+
def _process_tag_item(
71+
tags_data: dict, item: str | dict, tag_name: str, problem_tags_map: dict[str, list[str]]
72+
) -> None:
73+
if isinstance(item, dict) and "tag" in item:
74+
_process_tag_reference(tags_data, item, tag_name, problem_tags_map)
75+
elif isinstance(item, str):
76+
_add_problem_to_tag_map(problem_tags_map, item, tag_name)
77+
78+
6279
@lru_cache(maxsize=1)
6380
def _build_problem_tags_cache() -> dict[str, list[str]]:
6481
try:
@@ -68,17 +85,9 @@ def _build_problem_tags_cache() -> dict[str, list[str]]:
6885
problem_tags_map: dict[str, list[str]] = {}
6986

7087
for tag_name, problems in tags_data.items():
71-
if not isinstance(problems, list):
72-
continue
73-
74-
for item in problems:
75-
if isinstance(item, dict) and "tag" in item:
76-
# Include all problems from referenced tag
77-
for problem_name in tags_data.get(item["tag"], []):
78-
if isinstance(problem_name, str):
79-
_add_problem_to_tag_map(problem_tags_map, problem_name, tag_name)
80-
elif isinstance(item, str):
81-
_add_problem_to_tag_map(problem_tags_map, item, tag_name)
88+
if isinstance(problems, list):
89+
for item in problems:
90+
_process_tag_item(tags_data, item, tag_name, problem_tags_map)
8291

8392
return problem_tags_map
8493
except (ValueError, OSError, KeyError):

0 commit comments

Comments
 (0)