A few improvements following principles of Cleancode and Divide & Conquer:
- Split into small tasks, each one a function.
- Use meaningful names.
- Leverage the language's power.
Split into small tasks, each one a function
The challenge's description explicitly names 2 tasks, thus implemented in functions:
convert_to_binary(number)max_consecutive_1s_of_binary(binary)
Above functions already have meaningful names (see 2), expressing what they do.
Besides you have other tasks, given implicitly:
- Read a (decimal) number from STDIN
- Determine length of a sequence (of max consecutive 1s)
- Print number to STDOUT (length of max consecutive 1s)
These side-tasks can be implemented using existing functions (in Python).
Use meaningful names
From above functional design you can derive data-structures and variables. Name them to express what (in context of your program) they contain:
number_inputbinary_representationmax_consecutive_1slength_of_max_consecutive_1s
Leverage the language's power
Use already built-in functions where suitable:
- slicing Use
binand slicing for stripping of the binary marker; or useformat(number, 'b')directly - string-splitting by regexstring-splitting by regex for split the binary representation into consecutive 1s using
0as split-marker (very smart solutionsolution by Roman) - max for finding the maximum of a list based on some key criterion (here the length)