Jan-08-2021, 03:59 PM
(This post was last modified: Jan-08-2021, 03:59 PM by deanhystad.)
The solution for trailing zeros is actually contained in the description of binary gap; contiguous zeros surrounded by ones. So a gap will be preceded and followed by a 1.
set gap count and max gap to zero set saw_one to False. while number is greater than zero if number is odd this is the end of a gap. if saw_one Check if this was largest gap set gap count to zero set saw_one to True else increment gap count shift number rightYou have all the parts, but your logic on what to do when you see a 1 is a bit off. I see no reason to build a list of zeros and ones and then count using the list. Get rid of the list and count the zeros in the code that is getting the bits.