forked from jamil-said/code-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddBinary.py
More file actions
executable file
·53 lines (45 loc) · 1.48 KB
/
addBinary.py
File metadata and controls
executable file
·53 lines (45 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
""" addBinary
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100".
"""
class Solution:
def addBinary(self, a, b):
"""
:type a: str
:type b: str
:rtype: str
"""
return bin(int(a,2) + int(b,2))[2:]
print(Solution().addBinary("11", "1")) #100
print(Solution().addBinary("10100000100100110110010000010101111011011001101110111111111101000000101111001110001111100001101",
"110101001011101110001111100110001010100001101011101010000011011011001011101111001100000011011110011"))
#"110111101100010011000101110110100000011101000101011001000011011000001100011110011010010011000000000"
'''alternative: slower, more complicated
class Solution:
def addBinary(self, a, b):
"""
:type a: str
:type b: str
:rtype: str
"""
carry = 0
result = ['0' for x in range(len(a)+1 if len(a)>=len(b) else len(b)+1)]
for i in range(1, len(result)+1):
tempA = int(a[-i]) if (len(a))-i >= 0 else 0
tempB = int(b[-i]) if (len(b))-i >= 0 else 0
temp = tempA + tempB + carry
if temp == 3:
result[-i] = '1'
carry = 1
elif temp == 2:
carry = 1
elif temp == 1:
result[-i] = '1'
carry = 0
else:
carry = 0
return ''.join(result) if result[0] != '0' else ''.join(result)[1:]
'''