22import re
33import sys
44
5- ROOT = Path (__file__ ).resolve ().parents [1 ]
6-
7- PLATFORMS = ["leetcode" ]
8- DIFFICULTIES = ["easy" , "medium" , "hard" ]
9-
10- LANGUAGE_NAMES = {
11- "java" : "Java" ,
12- "python" : "Python" ,
13- "cpp" : "C++" ,
14- }
15-
16- SOLUTION_FILES = {
17- "java" : "Solution.java" ,
18- "python" : "solution.py" ,
19- "cpp" : "solution.cpp" ,
20- }
215
6+ ROOT = Path (__file__ ).resolve ().parents [1 ]
7+ PLATFORM = "leetcode"
8+ LANGUAGE = "java"
9+ LANGUAGE_LABEL = "Java"
10+ DIFFICULTIES = ("easy" , "medium" , "hard" )
11+ SOLUTION_FILE = "Solution.java"
12+ NOTES_FILE = "notes.md"
2213
23- def parse_problem_name (folder_name : str ):
24- match = re .match (r"^(\d+)[-_](.+)$" , folder_name )
2514
15+ def title_from_folder (folder_name : str ) -> str :
16+ match = re .match (r"^(\d{4})-(.+)$" , folder_name )
2617 if not match :
27- return None , folder_name . replace ( "-" , " " )
18+ return folder_name
2819
29- problem_id = match .group (1 )
30- title = match .group (2 ).replace ("-" , " " ).replace ("_" , " " )
20+ problem_id , slug = match .groups ()
21+ title = slug .replace ("-" , " " )
22+ return f"{ problem_id } . { title } "
3123
32- return int (problem_id ), f"{ problem_id } . { title } "
3324
34-
35- def path_link (path : Path ) -> str :
25+ def repo_path (path : Path ) -> str :
3626 return path .relative_to (ROOT ).as_posix ()
3727
3828
39- def main () :
29+ def build_rows () -> tuple [ list [ dict [ str , str ]], list [ str ]] :
4030 rows = []
4131 errors = []
32+ java_root = ROOT / PLATFORM / LANGUAGE
4233
43- for platform in PLATFORMS :
44- platform_dir = ROOT / platform
45-
46- if not platform_dir .exists ():
34+ for difficulty in DIFFICULTIES :
35+ difficulty_dir = java_root / difficulty
36+ if not difficulty_dir .exists ():
4737 continue
4838
49- for lang_dir in platform_dir .iterdir ():
50- if not lang_dir .is_dir ():
39+ for problem_dir in sorted ( difficulty_dir .iterdir (), key = lambda path : path . name ):
40+ if not problem_dir .is_dir ():
5141 continue
5242
53- lang = lang_dir .name
54- lang_name = LANGUAGE_NAMES .get (lang , lang )
55-
56- for difficulty in DIFFICULTIES :
57- difficulty_dir = lang_dir / difficulty
58-
59- if not difficulty_dir .exists ():
60- continue
61-
62- for problem_dir in sorted (difficulty_dir .iterdir ()):
63- if not problem_dir .is_dir ():
64- continue
65-
66- problem_id , problem_title = parse_problem_name (problem_dir .name )
67-
68- expected_solution = SOLUTION_FILES .get (lang )
69- solution_path = problem_dir / expected_solution if expected_solution else None
70- notes_path = problem_dir / "notes.md"
71-
72- if expected_solution and not solution_path .exists ():
73- errors .append (
74- f"Missing { expected_solution } : { path_link (problem_dir )} "
75- )
43+ solution_path = problem_dir / SOLUTION_FILE
44+ notes_path = problem_dir / NOTES_FILE
45+
46+ if not solution_path .exists ():
47+ errors .append (f"Missing { SOLUTION_FILE } : { repo_path (problem_dir )} " )
48+ if not notes_path .exists ():
49+ errors .append (f"Missing { NOTES_FILE } : { repo_path (problem_dir )} " )
50+
51+ rows .append (
52+ {
53+ "platform" : PLATFORM ,
54+ "difficulty" : difficulty ,
55+ "problem" : title_from_folder (problem_dir .name ),
56+ "language" : LANGUAGE_LABEL ,
57+ "solution" : (
58+ f"[{ SOLUTION_FILE } ]({ repo_path (solution_path )} )"
59+ if solution_path .exists ()
60+ else "-"
61+ ),
62+ "notes" : (
63+ f"[{ NOTES_FILE } ]({ repo_path (notes_path )} )"
64+ if notes_path .exists ()
65+ else "-"
66+ ),
67+ }
68+ )
7669
77- if not notes_path .exists ():
78- errors .append (
79- f"Missing notes.md: { path_link (problem_dir )} "
80- )
70+ return rows , errors
8171
82- solution_cell = (
83- f"[{ expected_solution } ]({ path_link (solution_path )} )"
84- if solution_path and solution_path .exists ()
85- else "—"
86- )
8772
88- notes_cell = (
89- f"[notes.md]({ path_link (notes_path )} )"
90- if notes_path .exists ()
91- else "—"
92- )
93-
94- rows .append ({
95- "platform" : platform .capitalize (),
96- "difficulty" : difficulty .capitalize (),
97- "problem" : problem_title ,
98- "language" : lang_name ,
99- "solution" : solution_cell ,
100- "notes" : notes_cell ,
101- "sort_id" : problem_id if problem_id is not None else 10 ** 9 ,
102- })
103-
104- rows .sort (key = lambda row : (row ["platform" ], row ["language" ], row ["difficulty" ], row ["sort_id" ], row ["problem" ]))
105-
106- progress = []
107- progress .append ("# Progress" )
108- progress .append ("" )
109- progress .append (f"Total solved: **{ len (rows )} **" )
110- progress .append ("" )
111- progress .append ("| Platform | Difficulty | Problem | Language | Solution | Notes |" )
112- progress .append ("|---|---|---|---|---|---|" )
73+ def render_progress (rows : list [dict [str , str ]]) -> str :
74+ lines = [
75+ "# Progress" ,
76+ "" ,
77+ "| Platform | Difficulty | Problem | Language | Solution | Notes |" ,
78+ "|---|---|---|---|---|---|" ,
79+ ]
11380
11481 for row in rows :
115- progress .append (
82+ lines .append (
11683 f"| { row ['platform' ]} | { row ['difficulty' ]} | { row ['problem' ]} | "
11784 f"{ row ['language' ]} | { row ['solution' ]} | { row ['notes' ]} |"
11885 )
11986
120- progress .append ("" )
87+ lines .append ("" )
88+ return "\n " .join (lines )
12189
122- (ROOT / "PROGRESS.md" ).write_text ("\n " .join (progress ), encoding = "utf-8" )
90+
91+ def main () -> int :
92+ rows , errors = build_rows ()
93+ (ROOT / "PROGRESS.md" ).write_text (render_progress (rows ), encoding = "utf-8" )
12394
12495 if errors :
12596 print ("Repository structure errors:" )
12697 for error in errors :
12798 print (f"- { error } " )
128- sys .exit (1 )
99+ return 1
100+
101+ return 0
129102
130103
131104if __name__ == "__main__" :
132- main ()
105+ sys . exit ( main () )
0 commit comments