Skip to content

Commit 9406cc7

Browse files
committed
Add --problem-folder-name argument
`--problem-folder-name` allows to specify the template to format problem folder names (eg. `01-two-sum`)
1 parent f01244e commit 9406cc7

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

leetcode_export/__main__.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ def parse_args():
1313
parser = argparse.ArgumentParser(description='Export LeetCode solutions', formatter_class=argparse.RawTextHelpFormatter)
1414
parser.add_argument('--cookies', type=str, help='set LeetCode cookies')
1515
parser.add_argument('--folder', type=str, default='.', help='set output folder')
16-
parser.add_argument('--problem-filename', type=str, default='${question_id} - ${title_slug}.html',
16+
parser.add_argument('--problem-folder-name', type=str, default='${question_id}-${title_slug}',
17+
help='problem folder name format')
18+
parser.add_argument('--problem-filename', type=str, default='${question_id}-${title_slug}.html',
1719
help='problem description filename format')
1820
parser.add_argument('--problem-content', type=str,
1921
default='<h1>${question_id} - ${title}</h1><h2>Difficulty: ${difficulty} - ' +
@@ -70,7 +72,8 @@ def main():
7072

7173
logging.info(args)
7274

73-
problem_template = Template(args.problem_filename)
75+
problem_folder_name_template = Template(args.problem_folder_name)
76+
problem_statement_template = Template(args.problem_filename)
7477
problem_content_template = Template(args.problem_content)
7578
submission_template = Template(args.submission_filename)
7679

@@ -90,7 +93,7 @@ def main():
9093
os.mkdir(args.folder)
9194
os.chdir(args.folder)
9295

93-
written_problems: Set[str] = set()
96+
title_slug_to_problem_folder_name: dict[str, str] = dict()
9497

9598
for submission in leetcode.get_submissions():
9699
if args.only_accepted and submission.status_display != 'Accepted':
@@ -99,18 +102,20 @@ def main():
99102
if args.language and submission.lang not in args.language:
100103
continue
101104

102-
if not os.path.exists(submission.title_slug):
103-
os.mkdir(submission.title_slug)
104-
os.chdir(submission.title_slug)
105-
106-
if submission.title_slug not in written_problems:
107-
problem = leetcode.get_problem(submission.title_slug)
108-
info_filename = problem_template.substitute(**problem.__dict__)
109-
if not os.path.exists(info_filename):
110-
info_file = open(info_filename, 'w+')
111-
info_file.write(problem_content_template.substitute(**problem.__dict__))
112-
info_file.close()
113-
written_problems.add(submission.title_slug)
105+
if submission.title_slug not in title_slug_to_problem_folder_name:
106+
problem_statement = leetcode.get_problem(submission.title_slug)
107+
problem_folder_name = problem_folder_name_template.substitute(**problem_statement.__dict__)
108+
title_slug_to_problem_folder_name[submission.title_slug] = problem_folder_name
109+
if not os.path.exists(problem_folder_name):
110+
os.mkdir(problem_folder_name)
111+
os.chdir(problem_folder_name)
112+
113+
problem_statement_filename = problem_statement_template.substitute(**problem_statement.__dict__)
114+
if not os.path.exists(problem_statement_filename):
115+
with open(problem_statement_filename, 'w+') as problem_statement_file:
116+
problem_statement_file.write(problem_content_template.substitute(**problem_statement.__dict__))
117+
else:
118+
os.chdir(title_slug_to_problem_folder_name[submission.title_slug])
114119

115120
sub_filename = submission_template.substitute(**submission.__dict__)
116121
if not os.path.exists(sub_filename):

0 commit comments

Comments
 (0)