Skip to content

Commit 0113df4

Browse files
committed
Fix Solution link generation: preserve files data in lookup
- Prefer TOML data (with files) over API data (without files) in lookup - Avoid adding duplicate problems from API when local TOML exists - Ensure Solution links are added when problems have solution files - Fix issue where API data overwrote TOML data in problems lookup
1 parent c9d0868 commit 0113df4

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

tools/ai-markmap-agent/src/leetcode_api.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from __future__ import annotations
88

99
import json
10+
import re
1011
from pathlib import Path
1112
from typing import Any, Dict
1213

@@ -87,8 +88,29 @@ def merge_leetcode_api_data(
8788
problem_data["difficulty"] = api_data.get("difficulty", "")
8889

8990
# 添加 API 中有但本地沒有的問題(作為參考)
91+
# 注意:只添加真正本地沒有的問題,避免覆蓋本地 TOML 資料
9092
for api_id, api_data in api_problems.items():
91-
if api_id not in merged:
93+
# 檢查是否已經存在(可能以不同的 key 格式存在,如 "0001_two_sum")
94+
# 標準化 api_id 以便比較
95+
normalized_api_id = api_id
96+
if isinstance(api_id, str) and api_id.isdigit():
97+
normalized_api_id = api_id.zfill(4)
98+
99+
# 檢查是否已經存在(檢查所有可能的 key 格式)
100+
exists = False
101+
for key in merged.keys():
102+
# 檢查 key 是否匹配(可能是 "0001" 或 "0001_two_sum")
103+
if key == normalized_api_id or key == api_id:
104+
exists = True
105+
break
106+
# 檢查 key 是否以 ID 開頭(如 "0001_two_sum")
107+
if isinstance(key, str):
108+
match = re.match(r'^(\d+)_', key)
109+
if match and match.group(1).zfill(4) == normalized_api_id:
110+
exists = True
111+
break
112+
113+
if not exists:
92114
# 只添加基本資訊,標記為 API 來源
93115
merged[api_id] = {
94116
"id": api_id,

tools/ai-markmap-agent/src/post_processing.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,41 @@ def _build_problems_lookup(self, problems: dict[str, Any]) -> dict[str, dict]:
100100
if problem_id_str.isdigit():
101101
# Store as 4-digit format: "0011"
102102
normalized_id = problem_id_str.zfill(4)
103-
lookup[normalized_id] = value
103+
# Only store if not exists, or if current value has files (prefer TOML data over API data)
104+
if normalized_id not in lookup:
105+
lookup[normalized_id] = value
106+
else:
107+
# Prefer value with files (TOML data) over value without files (API data)
108+
existing = lookup[normalized_id]
109+
existing_has_files = bool(existing.get("files", {}).get("solution"))
110+
current_has_files = bool(value.get("files", {}).get("solution"))
111+
if current_has_files and not existing_has_files:
112+
lookup[normalized_id] = value
104113

105114
# Store as integer string (no leading zeros): "11"
106115
int_id = str(int(problem_id_str))
107116
if int_id != normalized_id:
108-
lookup[int_id] = value
117+
if int_id not in lookup:
118+
lookup[int_id] = value
119+
else:
120+
# Prefer value with files
121+
existing = lookup[int_id]
122+
existing_has_files = bool(existing.get("files", {}).get("solution"))
123+
current_has_files = bool(value.get("files", {}).get("solution"))
124+
if current_has_files and not existing_has_files:
125+
lookup[int_id] = value
109126

110127
# Also store original format if different
111128
if problem_id_str != normalized_id and problem_id_str != int_id:
112-
lookup[problem_id_str] = value
129+
if problem_id_str not in lookup:
130+
lookup[problem_id_str] = value
131+
else:
132+
# Prefer value with files
133+
existing = lookup[problem_id_str]
134+
existing_has_files = bool(existing.get("files", {}).get("solution"))
135+
current_has_files = bool(value.get("files", {}).get("solution"))
136+
if current_has_files and not existing_has_files:
137+
lookup[problem_id_str] = value
113138

114139
# Debug: Store sample for first few problems
115140
if len(debug_samples) < 3:

0 commit comments

Comments
 (0)