Skip to content

Instantly share code, notes, and snippets.

@empeje
Last active December 17, 2017 11:13
Show Gist options
  • Save empeje/b24e3b0b6867903a14d820bcb822faf4 to your computer and use it in GitHub Desktop.
Save empeje/b24e3b0b6867903a14d820bcb822faf4 to your computer and use it in GitHub Desktop.
[SNIPPET-19] Codility - Binary Gap
def solution(N):
# write your code in Python 2.7
bin_representation = "{0:b}".format(N)
bin_representation = 'x' + bin_representation
bin_representation = bin_representation + 'x'
start_index = [i for i in range(0, len(bin_representation)) if ((bin_representation[i] == '1') and (bin_representation[i+1]=='0'))]
end_index = [i for i in range(0, len(bin_representation)) if ((bin_representation[i] == '1') and (bin_representation[i-1]=='0'))]
print(start_index)
print(end_index)
comparison = []
for i in range(len(end_index)):
compare = end_index[i]-start_index[i]-1
if compare > 0:
comparison.append(compare)
if (len(comparison)==0):
return 0
else:
return max(comparison)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment