We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Not very readable, but:
Use bin(n) to turn it into binary, strip('0b') from the front, split the number at each 0, leaving you with just strings of 1's.
Take the max (biggest) string of 1's, and then count how many 1's are in that string.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 10: Binary Numbers
You are viewing a single comment's thread. Return to all comments →
Another one-liner:
print(max(bin(n).strip('0b').split('0')).count('1'))
Not very readable, but: Use
bin(n)
to turn it into binary,strip('0b')
from the front,split
the number at each 0, leaving you with just strings of 1's.Take the
max
(biggest) string of 1's, and thencount
how many 1's are in that string.