python_leetcode_runner
Test your leetcode Python solutions locally.
Installation
pip install python-leetcode-runner
Usage
I have a beginner's guide over here
Say your solution file add_numbers.py looks like this:
class Solution:
def addNumbers(self, x: int, y: int) -> int:
return x + y
All you need to add to the file is a few test cases, usually provided to you in the leetcode question description:
class Solution:
def addNumbers(self, x: int, y: int) -> int:
return x + y
tests = [
(
(2, 4,), # input tuple
6, # output
),
(
(45, 67,), # input tuple
112, # output
),
]
Now, run the code locally by doing:
> pyleet add_numbers.py
Test 1 - ([1, 2, 3])......................................................PASSED
Test 2 - ([4, 5, 6, 7])...................................................PASSED
Custom Validators
In some questions, you don't just have to match expected output with function output. For eg, in some questions it might ask you to modify a list in-place, or some questions might have many acceptable answers.
For that case, you can provide your own custom validator function.
A validator is a function that receives 3 arguments:
method: your leetcode solution functioninputs: your test inputs tupleexpected: your expected test output value
To make assertions, you have to use assert statements in the following way:
assert output == expected, (output, expected) # this tuple is important!
For example, let's add custom validation to the addNumbers method:
class Solution:
def addNumbers(self, nums: list[int]) -> int:
return sum(nums)
tests = [
(
([1, 2, 3],), # input tuple
6, # output
),
(
([4, 5, 6, 7],), # input tuple
22, # output
),
]
def validator(addNumbers, inputs, expected):
nums = inputs[0]
output = addNumbers(nums)
assert output == expected, (output, expected)
Here's a more elaborate example, remove_duplicates:
class Solution:
def removeDuplicates(self, nums: list[int]) -> int:
offset = 0
values: set[int] = set()
for index, num in enumerate(nums):
nums[index - offset] = num
if num in values:
offset += 1
else:
values.add(num)
new_length = len(nums) - offset
return new_length
tests = [
(
([1, 1, 2],),
(2, [1, 2]),
),
(
([0, 0, 1, 1, 1, 2, 2, 3, 3, 4],),
(5, [0, 1, 2, 3, 4]),
),
]
def validator(removeDuplicates, inputs, outputs):
nums, = inputs
length, expected = outputs
new_length = removeDuplicates(nums)
assert length == new_length, (length, new_length)
assert nums[:new_length] == expected, (nums[:new_length], expected)
Run the file against sample inputs by doing:
> pyleet remove_duplicates.py
Test 1 - ([1, 2, 2])......................................................PASSED
Test 2 - ([0, 1, 2, 3, 4, 2, 2, 3, 3, 4]).................................PASSED
Code Snippets
If you're using VSCode, you can use the provided code snippets to help write the test cases faster.
Metadata
Release files for python-leetcode-runner 1.0.9
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| python_leetcode_runner-1.0.9.tar.gz | 6.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| python_leetcode_runner-1.0.9-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 14.8 kB
Release files / python_leetcode_runner-1.0.9.tar.gz
| Download URL | python_leetcode_runner-1.0.9.tar.gz |
|---|---|
| Size | 6.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a624a8a6fa9c3e0eded1701ebd71935f3eef1f8d21586f2a8ce05421ca8a66de
|
|
BLAKE2b-256 checksum How to use checksums |
452d009723fc86c276a8709c78ad1953b12ae4d6cd6b7c67379f230fba26761a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.9.5
|
Release files / python_leetcode_runner-1.0.9-py3-none-any.whl
| Download URL | python_leetcode_runner-1.0.9-py3-none-any.whl |
|---|---|
| Size | 8.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
31e4a0a8b78757aa6ae82607cbbb14ea6bf2fcd99be4c72756bc791108378293
|
|
BLAKE2b-256 checksum How to use checksums |
1fb476c64b702ed799b46a5bed46cb3a986808d53ca175a56f1d10b43bb81655
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.9.5
|