Skip to main content

Solve LeetCode problems in your local IDE

Project description

Leet2Local

PyPI Python CI License: MIT

A CLI tool that brings LeetCode problems into your local IDE. Fetch problem descriptions, solve in your editor, run tests locally, and submit directly to LeetCode — all from the terminal.

lc fetch 1 --lang python
lc run 1
lc submit 1

Features

  • Fetch any problem by number — creates a local directory with the description, code stub, and sample test cases
  • Run locally — executes your solution against sample test cases without touching LeetCode servers
  • Submit remotely — sends your code to LeetCode and polls for full results including hidden test cases, runtime, and memory percentile
  • Multi-language — Python, JavaScript, C++, and Java
  • VS Code integration — generates tasks.json for keyboard-shortcut access to all commands
  • Secure auth — session cookies stored in OS native keychain (Windows Credential Manager / macOS Keychain)

Installation

Requirements: Python 3.11+

pip install leet2local

Or install from source:

git clone https://github.com/dhineshtheprogrammer/leet2local.git
cd Leet2Local
pip install -e .

For development (includes pytest and ruff):

pip install -e ".[dev]"

Quick Start

1. Log in

Get your session cookies from your browser after logging in to leetcode.com:

  1. Open DevTools → Application → Cookies → leetcode.com
  2. Copy LEETCODE_SESSION and csrftoken
lc login

2. Fetch a problem

lc fetch 1              # Two Sum, defaults to Python
lc fetch 42 --lang cpp  # Trapping Rain Water in C++

This creates:

./leetcode/1-two-sum/
├── question.md        # Problem description in Markdown
├── solution.py        # Code stub from LeetCode
└── test_cases.json    # Sample inputs and expected outputs

3. Write your solution

Open solution.py in your editor and implement the function.

4. Run locally

lc run 1
┌─ Test Results ──────────────────────────────────┐
│ #  │ Status │ Details                           │
│ 0  │  PASS  │                                   │
│ 1  │  PASS  │                                   │
│ 2  │  FAIL  │ got: [] expected: [0, 1]          │
└────────────────────────────────────────────────-┘
✗ 1/3 test(s) failed

5. Submit to LeetCode

lc submit 1
┌─ Accepted ──────────────────────────────────────┐
│ Runtime: 52 ms (beats 87.3%)                    │
│ Memory:  16.4 MB (beats 62.1%)                  │
│ Tests:   54/54                                  │
└─────────────────────────────────────────────────┘

Commands

Command Description
lc login Store LeetCode session cookies securely
lc fetch <number> [--lang] Fetch problem and create local files
lc run <number> Run solution against sample test cases
lc submit <number> Submit to LeetCode and get full results
lc config Show current configuration
lc config --set key=value Update a config value
lc config --vscode-init Generate .vscode/tasks.json

Language Support

Language Flag Runtime Required
Python 3 --lang python python
JavaScript --lang javascript node
C++ --lang cpp g++
Java --lang java javac, java

Set your default language so you don't need --lang on every fetch:

lc config --set settings.default_language=javascript

Configuration

On first run, create .leet2local.toml in your project root:

[settings]
default_language = "python"   # python | javascript | cpp | java
problems_dir = "./leetcode"   # where problem directories are created
run_mode = "local"            # local | remote

[api]
graphql_endpoint = "https://leetcode.com/graphql"
request_timeout = 30
max_retries = 5

[runtime]
python_cmd = "python"
node_cmd = "node"
java_cmd = "java"
javac_cmd = "javac"
cpp_compiler = "g++"
runner_timeout = 10           # seconds per test execution

[display]
show_difficulty = true
show_tags = false             # hide tags to avoid spoilers
syntax_highlight = true

Config is loaded from the nearest .leet2local.toml walking up from the current directory, falling back to ~/.config/leet2local/config.toml.


VS Code Integration

Generate keyboard-shortcut tasks:

lc config --vscode-init

This creates .vscode/tasks.json with three tasks. Add to your keybindings.json:

[
  { "key": "ctrl+shift+r", "command": "workbench.action.tasks.runTask", "args": "LC: Run Local" },
  { "key": "ctrl+shift+s", "command": "workbench.action.tasks.runTask", "args": "LC: Submit" }
]

How It Works

lc fetch 1
  └─ GraphQL: questionList (slug resolution)
  └─ GraphQL: questionData (description, stub, test cases)
  └─ HTML → Markdown (BeautifulSoup + markdownify)
  └─ Write question.md, solution.py, test_cases.json

lc run 1
  └─ Read test_cases.json
  └─ Render Jinja2 test harness template
  └─ subprocess → python harness.py
  └─ Parse LEET_PASS / LEET_FAIL / LEET_ERROR output lines

lc submit 1
  └─ GraphQL: submitCode mutation → submission_id
  └─ Poll: checkSubmission every 2s
  └─ Display Rich result table

Project Structure

Leet2Local/
├── leet2local/
│   ├── cli.py              # Typer CLI entry point
│   ├── auth.py             # Login, keyring credential storage
│   ├── fetcher.py          # GraphQL client, file creation
│   ├── runner.py           # Local runner dispatcher
│   ├── submitter.py        # Remote submission + polling
│   ├── html_parser.py      # HTML → Markdown conversion
│   ├── models.py           # Pydantic data models
│   ├── config.py           # TOML config management
│   ├── queries.py          # GraphQL query strings
│   ├── rate_limiter.py     # Tenacity retry decorator
│   └── runners/
│       ├── base.py         # Abstract Runner base class
│       ├── python_runner.py
│       ├── javascript_runner.py
│       ├── cpp_runner.py
│       └── java_runner.py
├── templates/              # Jinja2 test harness templates
├── scripts/
│   └── generate_slug_map.py  # Build slug fallback map
└── tests/

Limitations

  • Sample test cases only for local runs — hidden test cases require lc submit
  • LeetCode's API is unofficial and undocumented; field names may change
  • C++ and Java require the respective compilers installed and on PATH
  • lc submit requires a valid session cookie (obtained via lc login)

Development

# Run tests
pytest

# Lint
ruff check .

# Regenerate slug fallback map (run once, requires internet)
python scripts/generate_slug_map.py

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

leet2local-0.1.0.tar.gz (73.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

leet2local-0.1.0-py3-none-any.whl (82.2 kB view details)

Uploaded Python 3

File details

Details for the file leet2local-0.1.0.tar.gz.

File metadata

  • Download URL: leet2local-0.1.0.tar.gz
  • Upload date:
  • Size: 73.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for leet2local-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c2bd03570de0e42331482457c2fe4f2b08692637306a8e99b24317b84c4a930b
MD5 dffe7e0f6f6a9c87a2b599e933f08d4a
BLAKE2b-256 54f7147a909feed8acb0a37ff51ebf62d892e2030f9de5cd29231eb57a0a70d9

See more details on using hashes here.

Provenance

The following attestation bundles were made for leet2local-0.1.0.tar.gz:

Publisher: publish.yml on dhineshtheprogrammer/leet2local

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file leet2local-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: leet2local-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 82.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for leet2local-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3afd7e1e388baad9d7671ddf359bcf509936d440366ee476bfd90c6b85f31622
MD5 53b263e20acc690d1846abfd835050fe
BLAKE2b-256 077e8230e5e210532fdb2b98abf2523e16746a6eb723072646e560ddc511d9c0

See more details on using hashes here.

Provenance

The following attestation bundles were made for leet2local-0.1.0-py3-none-any.whl:

Publisher: publish.yml on dhineshtheprogrammer/leet2local

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page