Skip to main content

Run LeetCode-style Python solutions against local TOML test cases.

Project description

Leet-Chaser

本地运行 leetcode solution 的命令行框架。

pip 安装运行

python -m pip install leet-chaser
leet-chaser --help

安装后可以直接使用 leet-chaser 命令:

leet-chaser init two-sum
leet-chaser run two-sum

uvx 直接运行

不想把命令安装到当前环境时,可以用 uvx 临时运行:

uvx leet-chaser --help
uvx leet-chaser init two-sum
uvx leet-chaser run two-sum

使用方法

STEP 1: 初始化题目目录

leet-chaser init two-sum

命令会在当前目录创建一个题目文件夹:

two-sum/
├── solution.py
└── cases.toml

STEP 2: 编写 solution.py

from typing import List


class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        seen = {}
        for index, num in enumerate(nums):
            rest = target - num
            if rest in seen:
                return [seen[rest], index]
            seen[num] = index
        return []

STEP 3: 填写 cases.toml

entrypoint 是 solution 里的入口方法名。input 是传给入口方法的位置参数数组,output 是期望返回值。

entrypoint = "twoSum"

[[cases]]
input = [[2, 7, 11, 15], 9]
output = [0, 1]

[[cases]]
input = [[3, 2, 4], 6]
output = [1, 2]

[[cases]]
input = [[3, 3], 6]
output = [0, 1]

链表题可以通过顶级类型元数据把数组解析成节点对象:

entrypoint = "reverseList"
input_types = ["linked_list"]
output_type = "linked_list"

[[cases]]
input = [[1, 2, 3]]
output = [3, 2, 1]

更多单链表、双向链表和循环链表写法见 docs/advanced-case-types.md

原地修改数组的题目可以通过顶级原地写入元数据比较被修改后的输入参数:

entrypoint = "moveZeroes"
inplace_write = true
inplace_index = 0

[[cases]]
input = [[0, 1, 0, 3, 12]]
output = [1, 3, 12, 0, 0]

inplace_index 使用 0-based 参数下标。开启 inplace_write 后,返回值会被忽略;如果返回值不是 None,命令行会打印 warning。

STEP 4: 运行验证

leet-chaser run two-sum

STEP 5: 调试单个用例

把当前要排查的用例写入 debug.toml,格式和 cases.toml 一致,但只保留一个 [[cases]]

entrypoint = "twoSum"

[[cases]]
input = [[2, 7, 11, 15], 9]
output = [0, 1]

运行 debug 命令会默认读取当前题目目录下的 debug.toml,并逐行打印入口函数的变量变化:

leet-chaser debug two-sum
leet-chaser debug two-sum -t seen -t rest
leet-chaser debug two-sum -c two-sum/custom-debug.toml

标准示例

仓库内置了 LeetCode 1. Two Sum 的标准示例:

uv run leet-chaser run examples/two-sum

也内置了 LeetCode 206. Reverse Linked List 示例,用来展示 linked_list 类型元数据:

uv run leet-chaser run examples/reverse-linked-list

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

leet_chaser-0.1.4.tar.gz (59.1 kB view details)

Uploaded Source

Built Distribution

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

leet_chaser-0.1.4-py3-none-any.whl (15.1 kB view details)

Uploaded Python 3

File details

Details for the file leet_chaser-0.1.4.tar.gz.

File metadata

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

File hashes

Hashes for leet_chaser-0.1.4.tar.gz
Algorithm Hash digest
SHA256 500668b0aa298881ef8dcdb8521ac3b9accfc0b1ced1953f024290adcdf8c10f
MD5 a5d03efdd79749343ed1ab7d2ec7025e
BLAKE2b-256 935b64d3dace2b8a3d8112d43ecf18a5c457b8185986467f83d051347dcad1d3

See more details on using hashes here.

Provenance

The following attestation bundles were made for leet_chaser-0.1.4.tar.gz:

Publisher: publish.yml on gawainx/leet-chaser

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

File details

Details for the file leet_chaser-0.1.4-py3-none-any.whl.

File metadata

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

File hashes

Hashes for leet_chaser-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 24d482e10bd8e2e3b71b0671ad726644854b1f738439dbb6716559130288041c
MD5 e8aa70afce592ab984bf039a48f5015f
BLAKE2b-256 5bec076cd09996a825ff4ad84a165a8cefa50047aa65c75873ec3fd59e1fd70d

See more details on using hashes here.

Provenance

The following attestation bundles were made for leet_chaser-0.1.4-py3-none-any.whl:

Publisher: publish.yml on gawainx/leet-chaser

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