Skip to content

Commit 4e259b5

Browse files
committed
feat: update
1 parent fd9db3d commit 4e259b5

File tree

10 files changed

+236
-14
lines changed

10 files changed

+236
-14
lines changed

.amazonq/plans/migrate_problems_to_new_template.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@ Migrate problems from `.templates/leetcode/json_old/` to `.templates/leetcode/js
44

55
## Migration Steps
66

7-
1. **Create JSON**: Analyze old format, create new template in `.templates/leetcode/json/`
7+
1. **Create JSON**: Analyze old format, create new template in `.templates/leetcode/json/` (reference `.templates/leetcode/examples/` for format)
88
2. **Generate**: `make p-gen PROBLEM=<name>`
99
3. **Lint**: `make p-lint PROBLEM=<name>` (fix JSON if fails, regenerate with `FORCE=1`)
1010
4. **Implement**: Look at `leetcode_old/` code - copy solution with all comments/notes and review test cases
1111
- **Important**: Re-copy solution after `p-gen` since it overwrites with TODO placeholders
1212
- **Multiple solutions**: If old code has alternative implementations (e.g., Solution, SolutionDFS, SolutionBFS),
1313
add parametrize to test all classes (see lru_cache as example)
1414
5. **Test**: `make p-test PROBLEM=<name>`
15-
6. **Enhance tests**: If only 2-3 cases, add edge cases (update JSON → regenerate → lint → test)
15+
6. **Enhance tests**: Review test coverage - check if cases cover edge scenarios:
16+
- Boundary values (min/max constraints, empty inputs)
17+
- Different input sizes/lengths
18+
- Zero/null cases, single elements
19+
- Error conditions, special values
20+
- If only 2-3 basic cases, add comprehensive edge cases (update JSON → regenerate with `FORCE=1` → lint → re-copy solution → test)
1621

1722
## Progress
1823

.github/workflows/ci-test-reproducibility.yml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,6 @@ jobs:
3535
- name: Install dependencies
3636
run: poetry install --no-interaction --no-ansi
3737

38-
# - name: Cache Graphviz
39-
# id: cache-graphviz
40-
# uses: actions/cache@0400d5f644dc74513175e3cd8d07132dd4860809 # v4.2.4
41-
# with:
42-
# path: /usr/bin/dot
43-
# key: graphviz-${{ runner.os }}
44-
45-
# - name: Install Graphviz
46-
# if: steps.cache-graphviz.outputs.cache-hit != 'true'
47-
# run: sudo apt-get update && sudo apt-get install -y graphviz
48-
4938
- name: Delete existing problems
5039
run: rm -rf leetcode/*/
5140

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{
2+
"problem_name": "add_binary",
3+
"solution_class_name": "Solution",
4+
"problem_number": "67",
5+
"problem_title": "Add Binary",
6+
"difficulty": "Easy",
7+
"topics": "Math, String, Bit Manipulation, Simulation",
8+
"_tags": { "list": ["grind-75"] },
9+
"readme_description": "Given two binary strings `a` and `b`, return *their sum as a binary string*.",
10+
"_readme_examples": {
11+
"list": [
12+
{ "content": "```\nInput: a = \"11\", b = \"1\"\nOutput: \"100\"\n```" },
13+
{ "content": "```\nInput: a = \"1010\", b = \"1011\"\nOutput: \"10101\"\n```" }
14+
]
15+
},
16+
"readme_constraints": "- `1 <= a.length, b.length <= 10^4`\n- `a` and `b` consist only of `'0'` or `'1'` characters.\n- Each string does not contain leading zeros except for the zero itself.",
17+
"readme_additional": "",
18+
"helpers_imports": "",
19+
"helpers_content": "",
20+
"helpers_run_name": "add_binary",
21+
"helpers_run_signature": "(solution_class: type, a: str, b: str)",
22+
"helpers_run_body": " implementation = solution_class()\n return implementation.add_binary(a, b)",
23+
"helpers_assert_name": "add_binary",
24+
"helpers_assert_signature": "(result: str, expected: str) -> bool",
25+
"helpers_assert_body": " assert result == expected\n return True",
26+
"solution_imports": "",
27+
"solution_contents": "",
28+
"solution_class_content": "",
29+
"test_imports": "import pytest\nfrom leetcode_py.test_utils import logged_test\nfrom .helpers import assert_add_binary, run_add_binary\nfrom .solution import Solution",
30+
"test_content": "",
31+
"test_class_name": "AddBinary",
32+
"test_class_content": " def setup_method(self):\n self.solution = Solution()",
33+
"_solution_methods": {
34+
"list": [
35+
{
36+
"name": "add_binary",
37+
"signature": "(self, a: str, b: str) -> str",
38+
"body": " # TODO: Implement add_binary\n return \"\""
39+
}
40+
]
41+
},
42+
"_test_helper_methods": {
43+
"list": [{ "name": "setup_method", "parameters": "", "body": "self.solution = Solution()" }]
44+
},
45+
"_test_methods": {
46+
"list": [
47+
{
48+
"name": "test_add_binary",
49+
"signature": "(self, a: str, b: str, expected: str)",
50+
"parametrize": "a, b, expected",
51+
"test_cases": "[('11', '1', '100'), ('1010', '1011', '10101'), ('0', '0', '0'), ('1', '1', '10'), ('1111', '1111', '11110'), ('1', '0', '1'), ('0', '1', '1'), ('1', '111', '1000'), ('111', '1', '1000'), ('1010', '1', '1011'), ('1111', '1', '10000')]",
52+
"body": " result = run_add_binary(Solution, a, b)\n assert_add_binary(result, expected)"
53+
}
54+
]
55+
},
56+
"playground_imports": "from helpers import run_add_binary, assert_add_binary\nfrom solution import Solution",
57+
"playground_setup": "# Example test case\na = '11'\nb = '1'\nexpected = '100'",
58+
"playground_run": "result = run_add_binary(Solution, a, b)\nresult",
59+
"playground_assert": "assert_add_binary(result, expected)"
60+
}

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PYTHON_VERSION = 3.13
2-
PROBLEM ?= accounts_merge
2+
PROBLEM ?= add_binary
33
FORCE ?= 0
44
COMMA := ,
55

leetcode/add_binary/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Add Binary
2+
3+
**Difficulty:** Easy
4+
**Topics:** Math, String, Bit Manipulation, Simulation
5+
**Tags:** grind-75
6+
7+
**LeetCode:** [Problem 67](https://leetcode.com/problems/add-binary/description/)
8+
9+
## Problem Description
10+
11+
Given two binary strings `a` and `b`, return _their sum as a binary string_.
12+
13+
## Examples
14+
15+
### Example 1:
16+
17+
```
18+
Input: a = "11", b = "1"
19+
Output: "100"
20+
```
21+
22+
### Example 2:
23+
24+
```
25+
Input: a = "1010", b = "1011"
26+
Output: "10101"
27+
```
28+
29+
## Constraints
30+
31+
- `1 <= a.length, b.length <= 10^4`
32+
- `a` and `b` consist only of `'0'` or `'1'` characters.
33+
- Each string does not contain leading zeros except for the zero itself.

leetcode/add_binary/__init__.py

Whitespace-only changes.

leetcode/add_binary/helpers.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def run_add_binary(solution_class: type, a: str, b: str):
2+
implementation = solution_class()
3+
return implementation.add_binary(a, b)
4+
5+
6+
def assert_add_binary(result: str, expected: str) -> bool:
7+
assert result == expected
8+
return True
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "imports",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"from helpers import assert_add_binary, run_add_binary\n",
11+
"from solution import Solution"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": null,
17+
"id": "setup",
18+
"metadata": {},
19+
"outputs": [],
20+
"source": [
21+
"# Example test case\n",
22+
"a = \"11\"\n",
23+
"b = \"1\"\n",
24+
"expected = \"100\""
25+
]
26+
},
27+
{
28+
"cell_type": "code",
29+
"execution_count": null,
30+
"id": "run",
31+
"metadata": {},
32+
"outputs": [],
33+
"source": [
34+
"result = run_add_binary(Solution, a, b)\n",
35+
"result"
36+
]
37+
},
38+
{
39+
"cell_type": "code",
40+
"execution_count": null,
41+
"id": "assert",
42+
"metadata": {},
43+
"outputs": [],
44+
"source": [
45+
"assert_add_binary(result, expected)"
46+
]
47+
}
48+
],
49+
"metadata": {
50+
"kernelspec": {
51+
"display_name": "leetcode-py-py3.13",
52+
"language": "python",
53+
"name": "python3"
54+
},
55+
"language_info": {
56+
"codemirror_mode": {
57+
"name": "ipython",
58+
"version": 3
59+
},
60+
"file_extension": ".py",
61+
"mimetype": "text/x-python",
62+
"name": "python",
63+
"nbconvert_exporter": "python3",
64+
"version": "3.13.7"
65+
}
66+
},
67+
"nbformat": 4,
68+
"nbformat_minor": 5
69+
}

leetcode/add_binary/solution.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
# Time: O(len(a) + len(b))
3+
# Space: O(len(a) + len(b))
4+
def add_binary(self, a: str, b: str) -> str:
5+
# int(a, 2) converts binary string to decimal: int("11", 2) → 3
6+
# bin() converts decimal to binary string with prefix: bin(3) → '0b11'
7+
# [2:] removes the '0b' prefix to get just binary digits
8+
return bin(int(a, 2) + int(b, 2))[2:]
9+
10+
11+
# Python Base Conversion:
12+
#
13+
# String → Integer (using int() with base parameter):
14+
# - int("1010", 2) → 10 (binary to decimal)
15+
# - int("ff", 16) → 255 (hex to decimal)
16+
# - int("10", 8) → 8 (octal to decimal)
17+
#
18+
# Integer → String (conversion functions add prefixes):
19+
# - bin(10) → '0b1010' (binary with '0b' prefix)
20+
# - hex(255) → '0xff' (hex with '0x' prefix)
21+
# - oct(8) → '0o10' (octal with '0o' prefix)
22+
#
23+
# These prefixes match Python literal syntax:
24+
# - 0b1010 = 10, 0xff = 255, 0o10 = 8
25+
#
26+
# For string problems, slice off the prefix: bin(n)[2:] gives just the digits.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import pytest
2+
3+
from leetcode_py.test_utils import logged_test
4+
5+
from .helpers import assert_add_binary, run_add_binary
6+
from .solution import Solution
7+
8+
9+
class TestAddBinary:
10+
def setup_method(self):
11+
self.solution = Solution()
12+
13+
@logged_test
14+
@pytest.mark.parametrize(
15+
"a, b, expected",
16+
[
17+
("11", "1", "100"),
18+
("1010", "1011", "10101"),
19+
("0", "0", "0"),
20+
("1", "1", "10"),
21+
("1111", "1111", "11110"),
22+
("1", "0", "1"),
23+
("0", "1", "1"),
24+
("1", "111", "1000"),
25+
("111", "1", "1000"),
26+
("1010", "1", "1011"),
27+
("1111", "1", "10000"),
28+
],
29+
)
30+
def test_add_binary(self, a: str, b: str, expected: str):
31+
result = run_add_binary(Solution, a, b)
32+
assert_add_binary(result, expected)

0 commit comments

Comments
 (0)