Skip to content

SWE-agent takes a GitHub issue and tries to automatically fix it, using your LM of choice. It can also be employed for offensive cybersecurity or competitive coding challenges. [NeurIPS 2024]

Doc: Add leetcode tutorial (#661)

Kilian Lieret31703fcc6b02
diff --git a/docs/usage/cl_tutorial.md b/docs/usage/cl_tutorial.md
index 5a4b7819..f07a7cf1 100644
--- a/docs/usage/cl_tutorial.md
+++ b/docs/usage/cl_tutorial.md
@@ -4,6 +4,7 @@ This tutorial walks you trough running SWE-agent from the command line.
Beginners might also be interested in the our web-based GUI (see [here](web_ui.md)).
This tutorial focuses on using SWE-agent as a tool to solve individual issues.
Benchmarking SWE-agent is covered [separately](benchmarking.md).
+Finally, we have a different tutorial for using SWE-agent for [coding challenges](coding_challenges.md).
## Getting started
diff --git a/docs/usage/coding_challenges.md b/docs/usage/coding_challenges.md
new file mode 100644
index 00000000..5eda8979
--- /dev/null
+++ b/docs/usage/coding_challenges.md
@@ -0,0 +1,147 @@
+# Using SWE-agent for coding challenges
+
+!!! tip "Command line tutorial"
+ We also provide a more general [command line tutorial](cl_tutorial.md), which covers
+ many more advanced features of SWE-agent, but focuses on its use for software engineering
+ problems.
+
+It is easy to use SWE-agent to do more than just software engineering.
+For example, you can tell SWE-agent to work on leetcode or humaneval-style problems.
+
+For this, put the problem you want to solve in a markdown file `problem.md`, for example:
+
+<details>
+<summary>Example leetcode challenge</summary>
+
+This is the <a href="https://leetcode.com/problems/first-missing-positive/">first missing positive</a> challenge.
+
+```markdown
+--8<-- "docs/usage/leetcode_example.md"
+```
+
+</details>
+
+Second, we need to specify a repository wherein SWE-agent will work.
+Here, we can simply create an empty folder, and add a `main.py` file
+
+```bash
+mkdir empty
+touch main.py
+```
+
+and potentially populate it with the problem stub
+
+```python
+from typing import List
+
+
+class Solution:
+ def firstMissingPositive(self, nums: List[int]) -> int:
+```
+
+!!! tip
+ If some imports (like `List`) are missing in the problem stub (like they oftentimes do
+ in leetcode) , SWE-agent will figure out how to add them. However, it might take an
+ additional step, so it's best to directly specify them.
+
+Make sure to commit all changes to the repository:
+
+```bash
+git add . && git commit -m "Add problem stub"
+```
+
+Now, we can let SWE-agent solve the problem:
+
+```bash
+python run.py \
+ --data_path problem.md \
+ --repo_path /path/to/empty \
+ --config_file config/coding_challenge.yaml \
+ --model gpt4 \
+ --cost_limit 3.0
+```
+
+<details>
+<summary>Output</summary>
+
+```
+--8<-- "docs/usage/leetcode_example.md"
+```
+
+</details>
+
+SWE-agent will typically conclude with a message like
+
+```
+INFO Trajectory saved to trajectories/fuchur/azure-gpt4__problem__coding_challenge__t-0.00__p-0.95__c-3.00__install-1/26d111.traj
+INFO Saved predictions to
+ trajectories/fuchur/azure-gpt4__problem__coding_challenge__t-0.00__p-0.95__c-3.00__install-1/all_preds.jsonl
+╭──────────────────────────── 🎉 Submission successful 🎉 ────────────────────────────╮
+│ SWE-agent has produced a patch that it believes will solve the issue you submitted! │
+│ Use the code snippet below to inspect or apply it! │
+╰─────────────────────────────────────────────────────────────────────────────────────╯
+
+ # The patch has been saved to your local filesystem at:
+ PATCH_FILE_PATH='/Users/fuchur/Documents/24/git_sync/SWE-agent/trajectories/fuchur/azure-gpt4__problem__coding_challenge__t-0.00__p-0
+ 5__c-3.00__install-1/patches/26d111.patch'
+ # Inspect it:
+ cat "${PATCH_FILE_PATH}"
+ # Apply it to a local repository:
+ cd <your local repo root>
+ git apply "${PATCH_FILE_PATH}"
+```
+
+This informs you of the location of the [trajectory](trajectories.md) and of the patch file that contains the solution.
+
+In our case, this looks like this:
+
+
+<details>
+<summary>Patch (solution)</summary>
+
+```patch
+diff --git a/main.py b/main.py
+index 1e7f08f..beabaa7 100644
+--- a/main.py
++++ b/main.py
+@@ -2,4 +2,37 @@ from typing import List
+
+
+ class Solution:
+- def firstMissingPositive(self, nums: List[int]) -> int:
++ # Implementation of the function to find the smallest missing positive integer
++ def firstMissingPositive(self, nums: List[int]) -> int:
++ n = len(nums)
++ # Mark numbers (num < 0) and (num > n) with a special marker number (n+1)
++ # We can ignore those because if missing number is in the range (1, n+1)
++ # it must be in the array
++ for i in range(n):
++ if nums[i] <= 0 or nums[i] > n:
++ nums[i] = n + 1
++
++ # Use index as a hash key and number sign as a presence detector
++ # For example, if nums[1] is negative that means that number `1` is present in the array
++ for i in range(n):
++ num = abs(nums[i])
++ if num <= n:
++ nums[num - 1] = -abs(nums[num - 1])
++
++ # Now the index of the first positive number is equal to the smallest missing positive number
++ for i in range(n):
++ if nums[i] > 0:
++ return i + 1
++
++ # If no positive numbers were found, the smallest missing positive is n+1
++ return n + 1
++# Adding test cases to verify the implementation of firstMissingPositive
++if __name__ == "__main__":
++ sol = Solution()
++ # Test case 1
++ assert sol.firstMissingPositive([1, 2, 0]) == 3, "Test case 1 failed"
++ # Test case 2
++ assert sol.firstMissingPositive([3, 4, -1, 1]) == 2, "Test case 2 failed"
++ # Test case 3
++ assert sol.firstMissingPositive([7, 8, 9, 11, 12]) == 1, "Test case 3 failed"
++ print("All test cases passed successfully.")
+```
+</details>
\ No newline at end of file
diff --git a/docs/usage/leetcode_example.md b/docs/usage/leetcode_example.md
new file mode 100644
index 00000000..26ae86f4
--- /dev/null
+++ b/docs/usage/leetcode_example.md
@@ -0,0 +1,28 @@
+Given an unsorted integer array nums.
+Return the smallest positive integer that is not present in nums.
+
+You must implement an algorithm that runs in O(n) time and uses O(1) auxiliary space.
+
+## Example 1:
+
+> Input: nums = [1,2,0]
+> Output: 3
+> Explanation: The numbers in the range [1,2] are all in the array.
+
+## Example 2:
+
+> Input: nums = [3,4,-1,1]
+> Output: 2
+> Explanation: 1 is in the array but 2 is missing.
+
+## Example 3:
+
+> Input: nums = [7,8,9,11,12]
+> Output: 1
+> Explanation: The smallest positive integer 1 is missing.
+
+## Constraints:
+
+1 <= nums.length <= 105
+-231 <= nums[i] <= 231 - 1
+
diff --git a/mkdocs.yml b/mkdocs.yml
index 53d65ffe..1b8cf613 100644
--- a/mkdocs.yml
+++ b/mkdocs.yml
@@ -63,6 +63,7 @@ nav:
- Usage:
- "usage/index.md"
- "Command line usage": "usage/cl_tutorial.md"
+ - "Solving coding challenges": "usage/coding_challenges.md"
- "Using the web UI": "usage/web_ui.md"
- "Trajectories": "usage/trajectories.md"
- "Trajectory inspector": "usage/inspector.md"