Skip to content

Commit e078daf

Browse files
committed
GH-167: Solve Leetcode 67
1 parent 66e3c99 commit e078daf

File tree

12 files changed

+120
-2
lines changed

12 files changed

+120
-2
lines changed

collections/leetcode/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
<td>64</td>
8282
<td>65</td>
8383
<td>66</td>
84-
<td>67</td>
84+
<td>🟢&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode67'>67</a></td>
8585
<td>68</td>
8686
<td>69</td>
8787
<td>70</td>

collections/leetcode/data.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ problems:
3636
- name: 49
3737
languages: cpp
3838
level: medium
39+
- name: 67
40+
languages: python
41+
level: easy
3942
- name: 78
4043
languages: cpp
4144
level: medium

problems/leetcode67/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
solution
2+
*.dSYM
3+
python/main/__pycache__

problems/leetcode67/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Leetcode Problem
2+
3+
## Usage
4+
5+
Test program
6+
7+
```
8+
# Run all tests
9+
python solution_test.py
10+
```

problems/leetcode67/data/1.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
11
2+
1

problems/leetcode67/data/1.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
100

problems/leetcode67/data/2.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
1010
2+
1011

problems/leetcode67/data/2.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
10101
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "67. Add Binary",
3+
"link": "https://leetcode.com/problems/add-binary/",
4+
"tags": [
5+
"Math",
6+
"String",
7+
"Bit Manipulation",
8+
"Simulation"
9+
]
10+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution:
2+
def addBinary(self, a: str, b: str) -> str:
3+
len_a = len(a)
4+
len_b = len(b)
5+
max_len = len_a
6+
if len_a > len_b:
7+
gap = len_a - len_b
8+
for i in range(0, gap):
9+
b = '0' + b
10+
elif len_a < len_b:
11+
max_len = len_b
12+
gap = len_b - len_a
13+
for i in range(0, gap):
14+
a = '0' + a
15+
temp = 0
16+
result = []
17+
a = list(a)
18+
b = list(b)
19+
a.reverse()
20+
b.reverse()
21+
for i, bit_a in enumerate(a):
22+
sum_bit = int(bit_a) + int(b[i]) + temp
23+
if sum_bit == 3:
24+
result.append('1')
25+
temp = 1
26+
elif sum_bit == 2:
27+
result.append('0')
28+
temp = 1
29+
elif sum_bit == 1:
30+
result.append('1')
31+
temp = 0
32+
elif sum_bit == 0:
33+
result.append('0')
34+
temp = 0
35+
if temp == 1:
36+
result.append('1')
37+
result.reverse()
38+
return ''.join(result)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import sys
2+
import unittest
3+
from os import listdir
4+
from os.path import abspath, dirname, join
5+
6+
python_source = dirname(dirname(abspath(__file__)))
7+
sys.path.append(python_source)
8+
from main.solution import Solution
9+
10+
test_data_directory = join(dirname(python_source), 'data')
11+
test_cases = set([data_file.split('.')[0] for data_file in listdir(test_data_directory)])
12+
13+
def read_input(input_file):
14+
with open(input_file, 'r') as f:
15+
a = f.readline().strip()
16+
b = f.readline().strip()
17+
return a, b
18+
19+
def read_output(output_file):
20+
with open(output_file, 'r') as f:
21+
k = f.readline().strip()
22+
return k
23+
24+
class TestSequenceMeta(type):
25+
def __new__(mcs, name, bases, dict):
26+
27+
def gen_test(input_file, output_file):
28+
def test(self):
29+
solution = Solution()
30+
nums, val = read_input(input_file)
31+
expected_k = read_output(output_file)
32+
actual_k = solution.addBinary(nums, val)
33+
self.assertEqual(actual_k, expected_k)
34+
return test
35+
36+
for test_case in test_cases:
37+
test_name = f'test_{test_case}'
38+
input_file = join(test_data_directory, f'{test_case}.in')
39+
output_file = join(test_data_directory, f'{test_case}.out')
40+
dict[test_name] = gen_test(input_file, output_file)
41+
return type.__new__(mcs, name, bases, dict)
42+
43+
class TestSequence(unittest.TestCase, metaclass=TestSequenceMeta):
44+
pass
45+
46+
if __name__ == '__main__':
47+
unittest.main()
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
solution
2-
*.dSYM
2+
*.dSYM
3+
python/main/__pycache__

0 commit comments

Comments
 (0)