Created
October 11, 2019 02:13
-
-
Save Weizhang2017/d78a30bb45007af1eaf0424e9773e2ef to your computer and use it in GitHub Desktop.
Calculate the largest number of zeros between ones in a binary number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def conver_to_binary(N): | |
remainder = list() | |
quotient = N // 2 | |
remainder.append(quotient%2) | |
while quotient > 0: | |
remainder.append(quotient%2) | |
quotient = quotient // 2 | |
return remainder[::-1] | |
binary = conver_to_binary(N) | |
zeros = 0 | |
length = 0 | |
for value in binary: | |
if value == 1: | |
if zeros > length: | |
length = zeros | |
zeros = 0 | |
else: | |
zeros += 1 | |
return length |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment