-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd.py
More file actions
225 lines (179 loc) · 5.71 KB
/
Copy pathadd.py
File metadata and controls
225 lines (179 loc) · 5.71 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
"""
Usage: python add.py <problem_number>
Creates a new problem file from the template in the main/ directory,
fetching the title, slug, difficulty, code snippet, and example tests from LeetCode.
"""
import sys
import os
import glob
import urllib.request
import urllib.error
import json
import re
import ast
LEETCODE_GRAPHQL = "https://leetcode.com/graphql"
LIST_QUERY = """
query {
problemsetQuestionList: questionList(
categorySlug: ""
limit: 1
skip: %d
filters: {}
) {
questions: data {
frontendQuestionId: questionFrontendId
titleSlug
title
difficulty
}
}
}
"""
DETAIL_QUERY = """
query {
question(titleSlug: "%s") {
codeSnippets { langSlug code }
exampleTestcaseList
metaData
content
isPaidOnly
}
}
"""
def graphql(query):
payload = json.dumps({"query": query}).encode()
req = urllib.request.Request(
LEETCODE_GRAPHQL,
data=payload,
headers={
"Content-Type": "application/json",
"Referer": "https://leetcode.com/",
"User-Agent": "Mozilla/5.0",
},
)
with urllib.request.urlopen(req, timeout=10) as resp:
return json.loads(resp.read())
def fetch_problem(number):
data = graphql(LIST_QUERY % (number - 1))
questions = data["data"]["problemsetQuestionList"]["questions"]
if not questions or int(questions[0]["frontendQuestionId"]) != number:
return None
return questions[0]
def fetch_details(slug):
data = graphql(DETAIL_QUERY % slug)
return data["data"]["question"]
def parse_outputs(html):
"""Extract expected output values from the problem HTML content."""
outputs = []
# New format: <div class="example-block"> with <span class="example-io">
new_blocks = re.findall(r'<div class="example-block">(.*?)</div>', html, re.DOTALL)
for block in new_blocks:
m = re.search(r"<strong>Output:</strong>\s*<span[^>]*>(.*?)</span>", block, re.DOTALL)
if not m:
continue
raw = re.sub(r"<[^>]+>", "", m.group(1)).strip()
normalized = raw.replace("true", "True").replace("false", "False").replace("null", "None")
try:
outputs.append(repr(ast.literal_eval(normalized)))
except Exception:
outputs.append(repr(raw))
if outputs:
return outputs
# Old format: <pre> blocks
blocks = re.findall(r"<pre>(.*?)</pre>", html, re.DOTALL)
for block in blocks:
m = re.search(r"<strong>Output:</strong>\s*(.+)", block)
if not m:
continue
raw = re.sub(r"<[^>]+>", "", m.group(1)).strip()
normalized = raw.replace("true", "True").replace("false", "False").replace("null", "None")
try:
outputs.append(repr(ast.literal_eval(normalized)))
except Exception:
outputs.append(repr(raw))
return outputs
def build_solution(snippet):
return snippet.rstrip() + "\n pass"
def build_tests(method_name, params, test_cases, outputs):
lines = []
for i, (raw, expected) in enumerate(zip(test_cases, outputs), 1):
lines.append(f" # Test {i}")
values = raw.split("\n")
call_args = []
for param, value in zip(params, values):
name = param["name"]
lines.append(f" {name} = {value}")
call_args.append(name)
call = f"sol.{method_name}({', '.join(call_args)})"
lines.append(f" assert {call} == {expected}")
lines.append("")
lines.append(' print("All tests passed.")')
return "\n".join(lines)
def main():
if len(sys.argv) != 2:
print("Usage: python add.py <problem_number>")
sys.exit(1)
try:
number = int(sys.argv[1])
except ValueError:
print(f"Error: '{sys.argv[1]}' is not a valid problem number.")
sys.exit(1)
python_dir = os.path.join(os.path.dirname(__file__), "main")
pattern = os.path.join(python_dir, f"{number:04d}_*.py")
existing = glob.glob(pattern)
if existing:
print(f"Problem {number} already exists: {os.path.basename(existing[0])}")
sys.exit(1)
print(f"Fetching problem {number} from LeetCode...")
try:
problem = fetch_problem(number)
if not problem:
print(f"Could not find problem {number} on LeetCode.")
sys.exit(1)
details = fetch_details(problem["titleSlug"])
if details.get("isPaidOnly"):
print(f"Problem {number} is locked behind LeetCode Premium.")
sys.exit(1)
except urllib.error.URLError as e:
print(f"Network error: {e}")
sys.exit(1)
title = problem["title"]
difficulty = problem["difficulty"]
slug = problem["titleSlug"]
link = f"https://leetcode.com/problems/{slug}/"
file_title = re.sub(r"[^a-z0-9]+", "_", title.lower()).strip("_")
py_snippet = next(
s["code"] for s in details["codeSnippets"] if s["langSlug"] == "python"
)
meta = json.loads(details["metaData"])
method_name = meta["name"]
params = meta["params"]
test_cases = details["exampleTestcaseList"]
outputs = parse_outputs(details["content"])
solution_class = build_solution(py_snippet)
tests_body = build_tests(method_name, params, test_cases, outputs)
content = f'''"""
Problem: {number} - {title}
Difficulty: {difficulty}
Link:
{link}
Time Complexity:
O(...)
Space Complexity:
O(...)
Notes:
"""
{solution_class}
def run_tests():
sol = Solution()
{tests_body}
if __name__ == "__main__":
run_tests()
'''
filename = f"{number:04d}_{file_title}.py"
dest = os.path.join(python_dir, filename)
with open(dest, "w") as f:
f.write(content)
print(f"Created: main/{filename} [{difficulty}]")
if __name__ == "__main__":
main()