-
Notifications
You must be signed in to change notification settings - Fork 353
/
Copy pathBinary_Numbers.py
32 lines (27 loc) · 1.08 KB
/
Binary_Numbers.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
'''
Aim: Given a base-10 integer, n, convert it to binary (base-2). Then find and print the
base-10 integer denoting the maximum number of consecutive 1's in n's binary representation.
'''
n = int(input()) # getting input
remainder = []
while n > 0:
rm = n % 2
n = n//2 # whenever decimal to binary conversion is done, the number is repeatedly divided by 2 until it could not be divided any further
remainder.append(rm) # appending the remainder that is obtained each time in a list
count,result = 0,0
for i in range(0,len(remainder)):
if remainder[i] == 0:
count = 0 # as soon as '0' is encountered, the counter is set to 0
else:
count += 1 # the counter increases every time consecutive ones are encountered
result = max(result,count) # maximum streak of 1's is chosen
print(result)
'''
Sample Test Case:
Input:
13
Output:
2
Explaination:
The binary representation of 13 is 1101, so the maximum number of consecutive 1's is 2.
'''