Skip to content

David-Jianguang-Ran/LeetCodePracticeHelper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LeetCode Practice Helper

A simple python program for running tests on algorithm challenge solutions.
Here is a Demo notebook hosted on Google Colab

Guide:

Install

No installation necessary, just import objects from base.py into your python file.

Setting Up and Running a Problem

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

  1. inherent from this LeetCodeProblem class
  2. override get_tests to return a tuple of test cases
  3. override solution function with the body of your solution
  4. make a new instance of your class and call instance.run()
  5. (optional) instance.print_result can be called repeatedly, this can be useful in ipython (maybe)

Testers

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)

Bonus: Linked Lists Utility

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"""

Bonus: Function return caching

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!

About

Simple Framwork for setting up a practice enviroment for algorithm problems.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages