Taskhackerrank.com Task: 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.
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.
This question has already been answered but it was in Ruby and I couldn't quite understand the concept, I am just starting to code. What can you suggest me to improve on this piece of code? I think its quite long considering the work it does or is this the efficient solution?
count = 0
max_value = 1
if __name__ == '__main__':
n = int(input())
binary_num = bin(n).split("b")
binary_num = binary_num[1]
print(binary_num)
for index, i in enumerate(binary_num):
if i == "1":
count += 1
next_item = index + 1
while next_item < len(binary_num):
if binary_num[next_item] == "1":
count += 1
counter = count
temp = counter
next_item += 1
if temp > max_value:
max_value = temp
else:
counter = count
count = 0
break
if next_item == (len(binary_num)):
break
print("count is =",max_value)
Challenge taken from Hackerrank.
https://www.hackerrank.com/challenges/30-binary-numbers/problem?isFullScreen=true