A script I’m using to solve problems on Leetcode in Python 3. It doesn’t support any other language.
mkdir yournamein the directory.- Run
bash ./watch.sh yourname - Add files
problem.pyandproblem.testcasesin theyournamedirectory. - Copy-paste example testcases from Leetcode problem description as is into the
problem.testcasesfile. - Copy-paste a solution stub from Leetcode to
problem.pyand write your code. - Save file, and the runner will execute it and print any errors or simply
OKif everything is correct.
I name my files like this: 31-next-permutation.py and 31-next-permutation.testcases.
You can add a def check_tle() to the solution file. I use it to generate large random examples to check if I’d get a Time Limit Exceeded error on Leetcode. The limit is 5 seconds and you can change it in watch.sh file.
The runner.py file doesn’t have any external dependencies. But watch.sh uses inotifywait, which on NixOS is included in the package named inotify-tools. It also uses systemd-run command to enforce the time limit. So if you use a system without systemd, you’d need to change that part in the script.
The script doesn’t support interactive problems yet, where you need to implement a class with several methods.
The format of test cases in a .testcases file is exactly like on Leetcode:
So as said earlier, you just copy-paste all examples from Leetcode in a .testcases file and add your own cases in the same format.
I don’t have a premium account on Leetcode, and to hide paywalled problems from the list of problems on Leetcode I run this JS snippet:
$$('span[data-original-title="Subscribe to unlock"]').forEach(s => {
s.parentElement.parentElement.parentElement.parentElement.style.display = 'none'
})