Skip to content

Commit 29d9be9

Browse files
python: add problem 67 and unittest
1 parent dd30832 commit 29d9be9

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

Python/sln_1_100/solution_61_70.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# -*- coding: utf-8 -*-
2+
3+
class Solution_61_70(object):
4+
def addBinary(self, a, b):
5+
"""
6+
67
7+
:type a: str
8+
:type b: str
9+
:rtype: str
10+
"""
11+
if len(a) > len(b):
12+
max_len = len(a)
13+
b = '0' * (len(a) - len(b)) + b
14+
else:
15+
max_len = len(b)
16+
a = '0' * (len(b) - len(a)) + a
17+
18+
result = []
19+
carry = 0
20+
for ch_a, ch_b in zip(a[::-1], b[::-1]):
21+
bit = int(ch_a) + int(ch_b) + carry
22+
if bit > 1:
23+
result.append(str(bit - 2))
24+
carry = 1
25+
else:
26+
result.append(str(bit))
27+
carry = 0
28+
29+
if carry == 1:
30+
result.append('1')
31+
32+
return ''.join(result[::-1])
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# -*- coding: utf-8 -*-
2+
from sln_1_100.solution_61_70 import Solution_61_70
3+
from unittest import TestCase
4+
class TestSolution_61_70(TestCase):
5+
def setUp(self):
6+
self.sln = Solution_61_70()
7+
8+
def test_addBinary(self):
9+
ret = self.sln.addBinary('11','1')
10+
print(ret)

0 commit comments

Comments
 (0)