Ease Contest with Code Generator

Hi Folks,

I enjoyed LeetCode contest, but I think it's more enjoyable if running all sample cases can be done instantly.
Currently, we need to copy-paste the sample cases 1 by 1 and compare the output.

Motivated by this, I made HTML parser to generate the code skeleton: https://github.com/gyosh/leekcode.

Then we can code our solution in that skeleton. The code skeleton has all sample cases embedded, which will be executed and compared against your solution's output.
The feedback if your solution passes all sample cases or not will be given all at once.

Take this problem as example: https://leetcode.com/contest/biweekly-contest-25/problems/kids-with-the-greatest-number-of-candies/

The skeleton looks like:

# Kids With the Greatest Number of Candies - LeetCode Contest
from collections import *
import math

# ----- BEGIN CUT HERE -----

class Solution:
    def kidsWithCandies(self, candies, extraCandies):
        # Your solution goes here

# ------ END CUT HERE ------

nTc = 0
passing = 0

def runTc(_name, candies, extraCandies, _expected):
    global nTc
    nTc += 1

    _answer = Solution().kidsWithCandies(candies, extraCandies)
    if _expected == _answer:
        return 1
    print('Error at `{}`'.format(_name))
    print('Expected: {}'.format(str(_expected)))
    print('Got     : {}\n'.format(str(_answer)))
    return 0


candies = [2,3,5,1,3]
extraCandies = 3
_expected = [True,True,True,False,True]
passing += runTc('Example 1:', candies, extraCandies, _expected)

candies = [4,2,1,1,2]
extraCandies = 1
_expected = [True,False,False,False,False]
passing += runTc('Example 2:', candies, extraCandies, _expected)

candies = [12,1,12]
extraCandies = 10
_expected = [True,False,True]
passing += runTc('Example 3:', candies, extraCandies, _expected)


if passing == nTc:
    print('No error!')
else:
    print('FAIL!!!')

Let's call that generated code as "solution.py".
We can complete the Solution class with our code. Following solution is example:

...
class Solution:
    def kidsWithCandies(self, candies, extraCandies):
        ans = []
        for c in candies:
            can = True
            for d in candies:
                if c+extraCandies < d:
                    can = False
                    break
            ans.append(can)
        return ans
...

We can run that and get the instant feedback if the solution passes all sample cases:

$ python3 solution.py
No error!

If some sample cases fail, the output will be like:

$ python3 solution.py
Error at `Example 2:`
Expected: [True, False, False, False, False]
Got     : [True, True, False, False, False]

FAIL!!!

and we can debug + run it again until no more failure and we're ready for submission.

To submit, simply cut the class portion and paste to LeetCode's editor.

I have been using this in the past 10+ contests (most of which is virtual, I'm new) and it works well.
I thought to share this with the community. Hopefully this can make your contest experience better.

That being said, this tool is unofficial and still on beta. There also no guarantee it will always work for future contest problems especially if the HTML structure change.
So use it at your own risk!

Lastly, feel free to report bug as issue or submit pull request on the Github page!

-- William Gozali

Comments (0)