-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.py
More file actions
executable file
·57 lines (42 loc) · 1.57 KB
/
init.py
File metadata and controls
executable file
·57 lines (42 loc) · 1.57 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
#!/usr/bin/env python3
import json
import os
import shutil
import sys
def zero_pad(num, width=4):
return str(num).zfill(width)
def main():
if len(sys.argv) != 2:
print("Usage: ./init.py <problem_number>")
sys.exit(1)
num_str = sys.argv[1]
try:
num = int(num_str)
except ValueError:
print("Problem number must be an integer.")
sys.exit(1)
# Load questions.json
with open("questions.json", "r") as f:
questions = json.load(f)
if num >= len(questions) or questions[num] is None:
print(f"Question number {num} not found in questions.json.")
sys.exit(1)
slug = questions[num]
folder_name = f"{zero_pad(num)}_{slug}"
target_dir = os.path.join("leetcode", folder_name)
if os.path.exists(target_dir):
print(f"Target directory {target_dir} already exists.")
sys.exit(1)
template_dir = os.path.join("leetcode", "0000_template")
shutil.copytree(template_dir, target_dir)
# Optionally, generate a minimal README.md with the problem number and slug
readme_path = os.path.join(target_dir, "README.md")
with open(readme_path, "w") as f:
f.write(f"# {num}. {slug.replace('-', ' ').title()}\n\n")
leetcode_url = f"https://leetcode.com/problems/{slug}/description/"
f.write(f"[🔗 LeetCode Link]({leetcode_url})\n\n")
# f.write("<!-- Add problem description here -->\n\n")
f.write(open(os.path.join(template_dir, "README.md")).read())
print(f"Created {target_dir} from template.")
if __name__ == "__main__":
main()