A CLI app to run any given Leetcode python solution.
Project description
python_leetcode_runner
Test your leetcode Python solutions locally.
Installation
pip install python-leetcode-runner
Usage
Say your solution file remove_duplicates.py
looks like this:
class Solution:
def addNumbers(self, nums: List[int]) -> int:
return sum(nums)
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, nums: List[int]) -> int:
return sum(nums)
tests = [
(
([1, 2, 3],), # input tuple
6, # output
),
(
([4, 5, 6, 7],), # input tuple
22, # 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 tupleoutput
: your test output value
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, output):
nums = inputs[0]
assert addNumbers(nums) == output
Here's a more sensible 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(method, inputs, outputs):
nums, = inputs
length, expected = outputs
new_length = method(nums)
assert length == new_length
assert 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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for python_leetcode_runner-1.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9c1ce102f0ded3def49222b20b5a9b4844671ceaec2cf96857c5f84d75c8f6bf |
|
MD5 | 9d7d271d2ab766e4802ea0ab76b671c9 |
|
BLAKE2b-256 | c09c8e599cde790e626f08deb5cf9c297439580ba74e892c83ebdf67c893ac86 |
Hashes for python_leetcode_runner-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d1840197547077cf7e3af1625f754b0397ad50759829aa10def3d9627ef11756 |
|
MD5 | eb9e18c3edd0ebb4f8dafe3cf403ba41 |
|
BLAKE2b-256 | 25af13e9e8f0b2ae145cd44100cb0bcc6e316441bc53f76f26dee5cb31a20806 |