Skip to content

Commit 0d339f6

Browse files
committed
feat(downloader): save solution source code
1 parent c1b1f8c commit 0d339f6

2 files changed

Lines changed: 37 additions & 4 deletions

File tree

leetcode/downloader.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ def __init__(self):
1717
exist_ok=True,
1818
)
1919

20+
@staticmethod
21+
def get_solution_filename(language: str) -> str:
22+
mapping = {
23+
"python3": "solution.py",
24+
"java": "Solution.java",
25+
"cpp": "solution.cpp",
26+
"c": "solution.c",
27+
"javascript": "solution.js",
28+
}
29+
30+
return mapping.get(language, "solution.txt")
31+
2032
@staticmethod
2133
def _format_folder_name(question_id: str, slug: str) -> str:
2234
"""
@@ -54,4 +66,23 @@ def create_problem_directory(self, detail) -> Path:
5466
exist_ok=True,
5567
)
5668

57-
return problem_dir
69+
return problem_dir
70+
71+
72+
def save_solution(self, detail) -> Path:
73+
"""
74+
Save the source code into the appropriate solution file.
75+
"""
76+
77+
filename = self.get_solution_filename(detail.language)
78+
79+
problem_dir = self.create_problem_directory(detail)
80+
81+
solution_file = problem_dir / filename
82+
83+
solution_file.write_text(
84+
detail.code,
85+
encoding="utf-8",
86+
)
87+
88+
return solution_file

main.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from leetcode.api import LeetCodeAPI
1111
from config.settings import Config
1212

13-
from leetcode.downloader import SubmissionDownloader
13+
from leetcode.downloader import SubmissionDownloader
1414

1515

1616
console = Console()
@@ -140,9 +140,11 @@ def main():
140140

141141
downloader = SubmissionDownloader()
142142

143-
folder = downloader.create_problem_directory(detail)
143+
solution_path = downloader.save_solution(detail)
144144

145-
console.print(f"\n[green]Directory created:[/green] {folder}")
145+
console.print(
146+
f"\n[bold green]Solution saved:[/bold green]\n{solution_path}"
147+
)
146148

147149
if __name__ == "__main__":
148150
main()

0 commit comments

Comments
 (0)