Show HN: Openleetcode – 本地 LeetCode 运行环境,测试用例直接存放在仓库中
Show HN: Openleetcode – Local LeetCode runner where tests live in the repo

原始链接: https://github.com/therepanic/openleetcode

**openleetcode** 是一个用 Haskell 构建的本地开源 LeetCode 运行器,旨在让开发者能够在本地针对透明的测试套件运行并测试解决方案。与专有平台不同,它将测试套件和特定语言的运行时模板保存在仓库内,从而确保了完全的控制权与可观测性。 该系统采用插件化执行后端(目前通过 Docker 使用 Piston),可以安全地运行多种语言的解决方案,包括 Python、Rust、C++ 和 Java。其命令行工具充当桥梁,允许用户通过题目 ID 或标题提交文件。 该项目的核心特性在于其可扩展设计:每个题目都包含一个清单和标准的解决方案文件,便于审计或贡献。项目包含辅助脚本,利用大语言模型(LLM)自动生成题目清单和测试用例,将 AI 视为“初级贡献者”,由其起草初步工作供人工审核。openleetcode 专为透明化设计,确保测试逻辑绝不会隐藏在黑盒之中,为编程练习提供了一个灵活且可重复的环境。

“OpenLeetCode” 是一款用 Haskell 编写的全新开源命令行工具,允许开发者在本地运行类似 LeetCode 风格的编程题目。该项目目前支持约 1400 道题目,涵盖 Python、Rust、Go 和 C++ 等多种编程语言,用户无需依赖专有平台 API 或互联网连接即可进行离线练习。 开发者 *therepanic* 开发此工具是为了解决编程竞赛平台缺乏透明度的问题,这些平台往往不公开测试用例。通过维护一套开放的、由社区驱动的测试用例,该项目提供了一种透明的替代方案,开发者可将这些练习整合到自己的工作流中,例如 Vim 插件或团队编程挑战。 虽然该工具目前处于最小可行性产品(MVP)阶段,尚不支持复杂的 SQL 或并发问题,但开发者强调,测试用例的开放性将使社区能够随着时间的推移不断提高覆盖率。Hacker News 社区对此反应热烈,用户普遍称赞了使用 Haskell 的选择以及其自定义集成的潜力,尽管对于在人工智能编程助手时代是否仍有必要进行 LeetCode 风格的练习,仍存在一些争议。
相关文章

原文

openleetcode
"There are no magic machines and no magic operators." - DHH

Made in Haskell Build with Cabal Supported problems ci License Unlicense

openleetcode is a local LeetCode runner built around open test suites, made in Haskell.

It takes a normal solution file, finds the matching problem manifest, builds a tiny language-specific harness, sends it to a pluggable execution backend, and judges the result locally. The tests live in the repo. The runtime templates live in the repo. The CLI is just the glue.

$ openleetcode submit ./solution.py --id 1
$ openleetcode submit ./solution.rs --title two-sum

openleetcode demo

You need Docker for the execution backend. On Linux and macOS the installer will try to start the default Piston backend for you through Docker Compose. On Windows, the installer only installs the CLI. Bring Docker yourself and start the backend manually.

Linux and macOS:

curl -fsSL https://raw.githubusercontent.com/therepanic/openleetcode/main/install.sh | sh

Windows PowerShell:

irm https://raw.githubusercontent.com/therepanic/openleetcode/main/install.ps1 | iex

Docker:

curl -fsSL https://raw.githubusercontent.com/therepanic/openleetcode/main/openleetcode.yml -o openleetcode.yml
curl -fsSL https://raw.githubusercontent.com/therepanic/openleetcode/main/backends/piston/docker-compose.yml -o piston.yml
docker compose -f piston.yml up -d
docker compose -f openleetcode.yml -f piston.yml run --rm openleetcode --version

The backend may take a while to install runtimes on the first start.

openleetcode currently uses Piston as its execution backend. The default config points to:

From a checkout, start the bundled backend with:

docker compose -f backends/piston/docker-compose.yml up -d

Then check the CLI config:

$ openleetcode config list
$ openleetcode config set backend.url http://localhost:2000

Download the latest public data assets:

$ openleetcode download all

Run a solution by problem id:

$ openleetcode submit ./two_sum.py --id 1

Run a solution by title:

$ openleetcode submit ./solution.cpp --title "two-sum"

Override language detection when the file extension is ambiguous:

$ openleetcode submit ./main.abc --id 1 --lang python3

Update openleetcode:

The runner has templates for:

cpp, rust, python3, python2, ruby, java, csharp, kotlin, go, dart, swift, typescript

Each runtime provides the small compatibility layer LeetCode problems tend to need: JSON output, arrays, matrices, linked lists, binary trees, etc. Imports and common libraries are kept close to the official LeetCode environments, so a solution should look like a normal LeetCode submission, not a custom openleetcode program.

For code changes, you need a Haskell toolchain with Cabal.

Build the CLI:

$ cabal build exe:openleetcode

Run the test suites:

$ cabal test core-tests
$ cabal test cli-tests

Run the built executable directly:

Contributing Without Code

Start with TEST_FORMAT.md. Seriously. It is the contract between the YAML, the runtime templates, and the judge.

Every problem is a directory with a manifest.yaml:

tests/1-500/1. two-sum/manifest.yaml
tests/1-500/1. two-sum/sol.py
tests/1-500/1. two-sum/sol.cpp

There are helper scripts too, because we are all human and writing the 39th edge case by hand is how people start bargaining with spreadsheets.

$ python generate_prompt.py two-sum
$ python spartan.py --skip 0 --limit 10 --no-generate --concurrency 5
$ python molotov.py --skip 0 --limit 10 --concurrency 5

generate_prompt.py builds a prompt for one LeetCode problem from its statement, code snippets, and a reference Python solution. spartan.py does the same in batches and can ask an LLM through OpenRouter to draft manifests into generated_problems/ when OPENROUTER_API_KEY is set. molotov.py fills in sol.{lang} files from those generated folders, reusing prompt.txt and sol.py. Treat the output like a junior contributor with infinite patience: useful, fast, and still very much in need of review.

openleetcode is young and some manifests will be better than others. That is fine. The whole point is that the judge and tests are not sealed away somewhere.

联系我们 contact @ memedata.com