-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddBinary.py
More file actions
40 lines (30 loc) · 766 Bytes
/
addBinary.py
File metadata and controls
40 lines (30 loc) · 766 Bytes
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
class Solution:
# @param a, a string
# @param b, a string
# @return a string
def addBinary(self, a, b):
a_dec = binToDec(a)
b_dec = binToDec(b)
sum = a_dec + b_dec
return decToBin(sum)
def binToDec(s):
num = [int(x) for x in list(s)]
sum = 0
for i in range(len(num)):
sum += num[i]*2**(len(num)-1-i)
return sum
def decToBin(num):
#import copy
#d = copy.copy(num)
d = num
sum = 0
i = 0
while d:
# 7:111 7%2 1 3%2 1 1%2 1
# 6:110 6%2 0 3%2 1 1%2 1
# 14:1110 14%2 0 7%2 1 3%2 1 1%2 1
dig = d % 2
d = d/2
sum += 10**i*dig
i += 1
return str(sum)