ChatGPT 3.5 does TDD oriented solutions of leetcode problems using Python3.
(based on https://github.com/gpeddle/leetcode-python)
This project uses the approach outlined below, with ChatGPT 3.5 as the code generator from a simple prompt sequence.
- Python3
- PyTest
Assumption: Python3 and PyTest are installed globally, so no need for virtual environments.
A new problem folder can be initialized using the init-problem.py script.
This script requires the following parameters:
- <problem_number> an integer that corresponds to the leetcode problem number.
- <problem_name> the name of the leetcode method used to invoke the solution
Note that the problem name must contain only letters and numbers. This allows us to emit valid Python modules that include the problem name. Consult leetcode to get the problem name before initializing.
For example, problem #1, 'Two Sum' has the following method definition:
def twoSum(self, input: List[int], target: int) -> List[int]:
... and so the problem name is 'twoSum'. When initializing this problem:
python3 init-problem.py 1 twoSum
Each problem is set up in a subdirectory under the algorithms subfolder.
The init-problem.py script does the following:
- Creates a new subdirectory for the problem
- Creates a
problem.mdfile - Creates a
prompt.mdfile - Creates a
response.mdfile - Creates a
solution.pyfile with a sample solution class - Creates a
tests_<problem_name>.pyfile with a table-driven test
- Navigate to the problem subdirectory.
- Copy/paste/format the problem description from leetcode into the
problem.mdfile. - Edit the tests file to make
test_datamatch the examples from the problem description. - Copy/paste the solution class method signature from leetcode into
solution.py- The method signature must match the invocation in the test script!
The prompt.md file has an initial instuction and two sections: PROBLEM and SKELETON. Each of the sections has a code fence just below the section heading. The code fences are in markdown and python respectively.
- Copy the contents of
problem.mdinto the area belowPROBLEM: - Copy the contents of
solution.mdinto the area belowSKELETON:
- Copy the contents of
prompt.mdinto a ChatGPT session. You do not need a new session because the prompt includes an initial reset instruction. - Click 'Send' to submit the prompt
- Examine the response
- Copy the entire response into the
response.mdfile. (Hint: use copy icon) - Copy the generated solution class into the
solution.pyfile.
- Navigate to the problem subdirectory.
- Run
pytest -vto test the solution - Revise the
prompt.mdfile if needed and resubmit to ChatGPT. - Repeat #2 and #3 to arrive at a solution that satisfies the examples provided by leetcode.
When the solution passes all the example test_data locally,
- Copy the contents of
solution.pyand paste it into leetcode's editor, then click [Run]. - Observe that leetcode runs the solution and check to output for errors.
- Review any errors and revisit the solution locally to correct the method.
Note that leetcode will only test your solution against the example
test_dataduring [Run].
Once you are satisfied that the solution passes tests proposed by the example data, click [Submit]. This submits the solution to the leetcode judge. the judge runs the solution with additional test data to verify the correctness and performance of the solution.
Note that the leetcode judge will usually impose additional edge cases which must be addressed. If your solution does not handle these cases, simply copy the new unexpected edge case input parameters to a new entries in
test_dataand return to working on the solution.
After completing a problem and passing the leetcode judge submission process, review the code folder and then commit to github. This maintains a clean set of solutions and a history of how you approached solving these problems.
Congratulations! Your next step is to choose a new problem and repeat this process.
Using this approach, I completed 10 problems in about an hour… but most of the time was spent on file copy/paste. The actual time the AI was involved is probably about 5 minutes.
ChatGPT got everything right on its first attempt.
I hope this approach aids you in hacking through leetcode problems, and especially that it elevates your happiness in coding.