A simple python program for running tests on algorithm challenge solutions.
Here is a Demo notebook
hosted on Google Colab
No installation necessary, just import objects from base.py into your python file.
Subclass base.LeetCodeProblem class to use the library.
A template.py is provided, simply copy and rename from template is usually all you'll need.
step-by-step user guide:
see example.py for code example
- inherent from this LeetCodeProblem class
- override get_tests to return a tuple of test cases
- override solution function with the body of your solution
- make a new instance of your class and call instance.run()
- (optional) instance.print_result can be called repeatedly, this can be useful in ipython (maybe)
Tester functions govern how output of a solution is checked against the expected case.
select tester by setting YourClass.tester to a key value below, either as class variable or instance variable
Here are some available testers:
- "exact" : output must exactly matches expected
- "any" : output must match one element in expected, expected must be a list or tuple
- "all" : output must have same elements as expected, expected must be a list or tuple
- "linked_lists" : output must have same val element-wise as expected, both output and expected needs to be head ListNode
- "object" : please see excercises/example_oo.py for details
You can also expand on the library by implementing your own testers and selecting it.
Here is an example:
from base import LeetCodeProblem, make_tester
class YourClass(LeetCodeProblem):
# select your tester by key
tester = "all"
# define your tester here
@make_tester(LeetCodeProblem._testers, key="all")
def many_to_many(self, truth, result):
if not isinstance(result, (list, tuple)):
raise TypeError("expected output must be a list or tuple")
assert sorted(truth) == sorted(result)
This library also includes utility function for converting python lists to linked lists. Enjoy!
base.to_linked_list (plist : list) -> ListNode: """takes python list and return head of linked list"""
base.to_plist (head : ListNode) -> list: """scans from head onwards, returns python list"""
You can use this decorator to cache the return of a specific function so that result is only computed once for each unique arg
Note: caching functions with kwargs is not supported at the moment
base.cached (__cache : dict) -> python function decorator:
"""cache returns of decorated function in __cache, decoratee function can only have args not kwargs"""
example:
from base import cached
# declare a dictionary as a cache
cache = {}
# define your function and decorate with base.cached
@cached(cache)
def factorial(number):
return factorial(number - 1) * number
# enjoy!