-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_problems.py
More file actions
274 lines (230 loc) · 8.26 KB
/
Copy pathupdate_problems.py
File metadata and controls
274 lines (230 loc) · 8.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
"""
update_problems.py
------------------
Scans JAVA/LeetCode/ for solved problems, fetches
title + difficulty from LeetCode GraphQL API, gets
commit date from git log, then writes PROBLEMS.md.
Place in: java_progress/.github/scripts/
"""
import os
import re
import json
import subprocess
import requests
from datetime import datetime
README_PATH = "README.md"
PROBLEMS_PATH = "PROBLEMS.md"
LC_BASE = "src/LeetCode"
HEADERS = {
"Content-Type": "application/json",
"Referer": "https://leetcode.com",
"User-Agent": "Mozilla/5.0",
}
DIFFICULTY_EMOJI = {"Easy": "🟢", "Medium": "🟡", "Hard": "🔴"}
def extract_problem_number(filename: str) -> int | None:
"""Extract LeetCode problem number from filename."""
# Patterns: LeetCode_1_TwoSum.java, LeetCode_704.java, LeetCode_33x5.java
m = re.search(r"LeetCode[_\-](\d+)", filename, re.IGNORECASE)
return int(m.group(1)) if m else None
def scan_problems(base_path: str) -> dict[int, dict]:
"""
Walk JAVA/LeetCode/ and collect unique problem numbers.
Returns {number: {folder, files, first_file_path}}
"""
problems: dict[int, dict] = {}
if not os.path.isdir(base_path):
print(f"[!] {base_path} not found.")
return problems
for folder in os.listdir(base_path):
folder_path = os.path.join(base_path, folder)
if not os.path.isdir(folder_path):
continue
for fname in os.listdir(folder_path):
if not fname.endswith(".java"):
continue
num = extract_problem_number(fname)
if num is None:
continue
fpath = os.path.join(folder_path, fname)
if num not in problems:
problems[num] = {
"folder": folder,
"file_path": fpath,
"file_name": fname,
}
return problems
def get_commit_date(file_path: str) -> str:
"""Get date of first commit for a file via git log."""
try:
result = subprocess.run(
["git", "log", "--follow", "--format=%as", "--", file_path],
capture_output=True, text=True, timeout=10
)
lines = [l.strip() for l in result.stdout.strip().splitlines() if l.strip()]
# Last line = oldest commit
return lines[-1] if lines else "—"
except Exception:
return "—"
def fetch_problem_details(number: int) -> dict:
"""Fetch problem title and difficulty from LeetCode GraphQL."""
query = """
query($titleSlug: String!) {
question(titleSlug: $titleSlug) {
title
difficulty
titleSlug
}
}"""
# First get titleSlug from problem number
list_query = """
query problemsetQuestionList($skip: Int!, $limit: Int!) {
problemsetQuestionList: questionList(
categorySlug: ""
limit: $limit
skip: $skip
filters: {}
) {
questions: data {
questionFrontendId
title
titleSlug
difficulty
}
}
}"""
try:
# Use the direct question endpoint by frontendId
r = requests.post(
"https://leetcode.com/graphql",
json={
"query": """
query($num: Int!) {
question: problemByFrontendId(id: $num) {
title difficulty titleSlug
}
}""",
"variables": {"num": number}
},
headers=HEADERS,
timeout=8,
)
data = r.json().get("data", {}).get("question")
if data:
return data
except Exception:
pass
# Fallback: try title_slug from the number directly
return {"title": f"Problem {number}", "difficulty": "Unknown", "titleSlug": str(number)}
def fetch_all_details(problem_numbers: list[int]) -> dict[int, dict]:
"""Batch-fetch problem details. Uses a single API call for the full list."""
details: dict[int, dict] = {}
print(f" Fetching details for {len(problem_numbers)} problems...")
# LeetCode GraphQL — get all problems in one call
try:
r = requests.post(
"https://leetcode.com/graphql",
json={
"query": """
query {
allQuestions {
questionFrontendId
title
titleSlug
difficulty
}
}"""
},
headers=HEADERS,
timeout=20,
)
all_q = r.json().get("data", {}).get("allQuestions", [])
for q in all_q:
try:
num = int(q["questionFrontendId"])
details[num] = {
"title": q["title"],
"difficulty": q["difficulty"],
"titleSlug": q["titleSlug"],
}
except (ValueError, KeyError):
continue
print(f" Fetched {len(details)} problems from LeetCode API.")
except Exception as e:
print(f" [!] Batch fetch failed: {e}. Using fallbacks.")
# Fill missing with individual fetch
missing = [n for n in problem_numbers if n not in details]
for num in missing:
details[num] = fetch_problem_details(num)
return details
def build_problems_md(
problems: dict[int, dict],
details: dict[int, dict],
dates: dict[int, str],
) -> str:
"""Build the full PROBLEMS.md content."""
updated = datetime.utcnow().strftime("%d %b %Y, %H:%M UTC")
total = len(problems)
easy = sum(1 for n in problems if details.get(n, {}).get("difficulty") == "Easy")
medium = sum(1 for n in problems if details.get(n, {}).get("difficulty") == "Medium")
hard = sum(1 for n in problems if details.get(n, {}).get("difficulty") == "Hard")
rows = []
for num in sorted(problems.keys()):
d = details.get(num, {})
title = d.get("title", f"Problem {num}")
diff = d.get("difficulty", "Unknown")
slug = d.get("titleSlug", str(num))
folder = problems[num]["folder"]
date = dates.get(num, "—")
emoji = DIFFICULTY_EMOJI.get(diff, "⚪")
lc_url = f"https://leetcode.com/problems/{slug}/"
rows.append(f"| {num} | [{title}]({lc_url}) | {emoji} {diff} | `{folder}` | {date} |")
table = "\n".join(rows)
return f"""# 🧠 LeetCode Solutions
> All **{total}** problems solved in Java — auto-updated on every push.
> Last updated: {updated}
## 📊 Summary
| 🟢 Easy | 🟡 Medium | 🔴 Hard | Total |
|:-------:|:---------:|:-------:|:-----:|
| {easy} | {medium} | {hard} | {total} |
---
## 📋 Problem List
| # | Problem | Difficulty | Topic | Date Solved |
|---|---------|------------|-------|-------------|
{table}
---
> Auto-generated by [GitHub Actions](../.github/workflows/update_problems.yml)
> Source: [java_progress/JAVA/LeetCode](JAVA/LeetCode/)
"""
def update_readme_link():
"""Add a link to PROBLEMS.md in README if not already there."""
if not os.path.isfile(README_PATH):
return
with open(README_PATH, "r", encoding="utf-8") as f:
content = f.read()
marker = "📋 [View All Solved Problems](PROBLEMS.md)"
if marker not in content:
# Insert after the DSA progress header
content = content.replace(
"## 📈 DSA Progress",
f"## 📈 DSA Progress\n\n> {marker}\n"
)
with open(README_PATH, "w", encoding="utf-8") as f:
f.write(content)
print("[+] README link added.")
def main():
print("Scanning JAVA/LeetCode/...")
problems = scan_problems(LC_BASE)
print(f" Found {len(problems)} unique problems.")
numbers = sorted(problems.keys())
details = fetch_all_details(numbers)
print("Getting commit dates...")
dates: dict[int, str] = {}
for num, info in problems.items():
dates[num] = get_commit_date(info["file_path"])
md = build_problems_md(problems, details, dates)
with open(PROBLEMS_PATH, "w", encoding="utf-8") as f:
f.write(md)
print(f"[+] {PROBLEMS_PATH} written — {len(problems)} problems.")
update_readme_link()
if __name__ == "__main__":
main()