-
Notifications
You must be signed in to change notification settings - Fork 0
/
371_sum_of_two_integers.py
115 lines (100 loc) · 3.42 KB
/
371_sum_of_two_integers.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
class Solution:
def getSum(self, a: 'int', b: 'int') -> 'int':
def addBits(bit1, bit2, carry):
#returns (val, carry)
if bit1 == bit2 == '1':
if carry == '0':
return('0','1')
elif carry == '1':
return('1','1')
elif bit1 == bit2 == '0':
if carry == '0':
return('0','0')
elif carry == '1':
return('1','0')
else:
if carry == '0':
return('1','0')
else:
return('0','1')
def determineBit(bit1, result, carry):
#Given bit1, the result, and carry,
#determines the bit X and the carry
#value newCarry that satisfy the relation
#result, newCarry = addBits(bit1, X, carry).
#Return value is (X, newCarry)
if bit1 == carry == '1':
if result == '0':
return('0', '1')
if result == '1':
return('1', '1')
elif bit1 == carry == '0':
if result == '0':
return('0', '0')
if result == '1':
return('1', '0')
else:
if result == '0':
return('1', '1')
if result == '1':
return('0', '0')
def addBinWords(x, y):
#Assumes both words x and y are the same length.
carry = '0'
added = ''
L = len(x)
for i in range(L):
val, carry = addBits(x[L-i-1], y[L-i-1], carry)
added = val+added
if carry == '1':
added = '1'+added
return(added)
def subtractBinWords(x, y):
#Assumes both words x and y are the same length,
#and y < x.
carry = '0'
result = ''
L = len(x)
for i in range(L):
val, carry = determineBit(x[L-i-1], y[L-i-1], carry)
result = val+result
if carry == '1':
result = '1'+result
return(result)
sign_x, x = bin(a).split('b')
sign_y, y = bin(b).split('b')
len_x = len(x)
len_y = len(y)
if len_x >= len_y:
y = (len_x-len_y)*'0'+y
#ensures that len_x = len_y.
else:
x = (len_y-len_x)*'0'+x
if sign_x == sign_y:
added = addBinWords(x,y)
if sign_x == '-0':
return(-int(added, 2))
else:
return(int(added, 2))
else:
if abs(a) >= abs(b):
result = subtractBinWords(y, x)
if sign_x == '-0':
return(-int(result, 2))
else:
return(int(result, 2))
else:
result = subtractBinWords(x, y)
if sign_x == '-0':
return(int(result, 2))
else:
return(-int(result, 2))
if __name__ == '__main__':
sol = Solution()
assert(sol.getSum(4, 5) == 9)
assert(sol.getSum(0, 10) == 10)
assert(sol.getSum(4, -5) == -1)
assert(sol.getSum(-3,-2) == -5)
assert(sol.getSum(0, 0) == 0)
assert(sol.getSum(-0, 0) == 0)
assert(sol.getSum(-0, -0) == 0)