Skip to content

Instantly share code, notes, and snippets.

@gbazilio
Last active January 16, 2017 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gbazilio/95556456e560961c3158b16c4df84f72 to your computer and use it in GitHub Desktop.
Save gbazilio/95556456e560961c3158b16c4df84f72 to your computer and use it in GitHub Desktop.
Codility - Binary Gap
def solution(N):
if N < 0: return 0
binary_string = bin(N)[2:]
max_gap_count = current_gap_count = 0
for index in range(len(binary_string)):
if binary_string[index] == "0":
current_gap_count += 1
else:
max_gap_count = max(current_gap_count, max_gap_count)
current_gap_count = 0
return max_gap_count
import re
def solution(N):
if N < 0: return 0
return len(max(re.sub(r'0+$', "", bin(N)[2:]).split('1')))
def solution(N):
N_string = bin(N)[2:]
current_zeros_count = 0
max_zeros_found = 0
for digit in N_string:
if digit == "1":
max_zeros_found = max(max_zeros_found, current_zeros_count)
current_zeros_count = 0
continue
current_zeros_count += 1
return max_zeros_found
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment