Skip to content

Instantly share code, notes, and snippets.

@alexandru-dinu
Last active November 4, 2021 14:43
Show Gist options
  • Save alexandru-dinu/6e6a42c1a44bcbd68084625ebf3769d9 to your computer and use it in GitHub Desktop.
Save alexandru-dinu/6e6a42c1a44bcbd68084625ebf3769d9 to your computer and use it in GitHub Desktop.
Get the most recent accepted submissions from leetcode.
"""
1) Fetch submisssions using
https://github.com/world177/Leetcode-Downloader-for-Submissions
2) Use this script to move the *latest accepted* submission to ./out/<problem>
3) Replace "// " comments from the first line of all python files
f in $(ls **/*.py); do sed -i.bak "1 s/\/\/ /# /" $f; done
"""
import shutil
from pathlib import Path
from datetime import datetime
datetime_fmt = r"%m-%d-%Y, %I:%M:%S %p"
out = Path("./out")
out.mkdir(parents=True, exist_ok=True)
for d in Path("leetcode").iterdir():
if not (d / "Accepted").exists():
print(f"No accepted solutions for {d.name}! Skipping...")
continue
latest = max(
(d / "Accepted").iterdir(),
key=lambda f: datetime.strptime(f.name, datetime_fmt),
)
sol = [f for f in latest.iterdir() if f.suffix != ".txt"]
assert len(sol) == 1, sol
src = sol[0]
dst = (out / d.name.replace("-", "_")).with_suffix(src.suffix)
shutil.copy(src, dst)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment