-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCTCI_5_6.py
More file actions
32 lines (26 loc) · 804 Bytes
/
CTCI_5_6.py
File metadata and controls
32 lines (26 loc) · 804 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
def int_to_binary ( int1 ):
binary = ""
while int1 > 0:
if int1 % 2 == 0:
binary = "0" + binary
else:
binary = "1" + binary
int1 /= 2
return binary
def add_zeros(bit, length):
length = length - len(bit)
for i in range(length):
bit = "0" + bit
return bit
def conversion ( int1, int2 ):
(binary1, binary2) = (int_to_binary(int1), int_to_binary(int2) )
shorter_bit = min([binary1, binary2], key = len)
longer_bit = max([binary1, binary2], key = len)
if len(longer_bit) > len(shorter_bit):
shorter_bit = add_zeros(shorter_bit, len(longer_bit))
counter = 0
for b1, b2 in zip(shorter_bit, longer_bit):
if b1 != b2:
counter += 1
return counter
print conversion(29, 15)