Skip to content

Instantly share code, notes, and snippets.

@sharatsachin
Created November 7, 2023 06:47
Show Gist options
  • Save sharatsachin/dbeb1d2de5cbc51061578c03ed50964e to your computer and use it in GitHub Desktop.
Save sharatsachin/dbeb1d2de5cbc51061578c03ed50964e to your computer and use it in GitHub Desktop.
Take a folder containing your leetcode solutions and commit them in the past.
# print the list of all files in the leetcode directory
import os
import pathlib
import glob
import itertools
files = pathlib.Path("leetcode")
file_ext = ['cpp', 'py', 'postgresql']
new_folder = './leetcode-solutions'
mp = {}
for ext in file_ext:
mp[ext] = {}
for file in list(files.rglob("*")):
if 'Accepted' not in str(file):
continue
for ext in file_ext:
if f'.{ext}' in str(file):
mp[ext][str(file).split('Accepted')[0]] = str(file)
if not os.path.exists(new_folder):
pathlib.Path(new_folder).mkdir(parents=True, exist_ok=True)
os.system(f'git -C {new_folder} init')
os.system(f'git -C {new_folder} commit -m --allow-empty "Initial commit"')
to_iter = itertools.chain(*[mp[ext].items() for ext in file_ext])
for key, value in to_iter:
problem_name = value.split('/')[1]
commit_date = value.split('/')[3].split(', ')[0]
commit_time = value.split('/')[3].split(', ')[1].split(' ')[0].replace('_', ':')
file_name = value.split('/')[-1]
file_name.replace('postgresql', 'sql')
new_file_path = f'{new_folder}/{problem_name}/{file_name}'
os.system(f'mkdir -p {new_folder}/{problem_name}')
os.system(f'cp "{value}" {new_file_path}')
os.system(f'git -C {new_folder} add {problem_name}')
os.system(f'git -C {new_folder} commit --date="{commit_date} {commit_time}" -m "Add solution for {problem_name}"')
os.system(f'git -C {new_folder} rebase --committer-date-is-author-date HEAD~1')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment