Last active
January 16, 2017 23:29
-
-
Save gbazilio/95556456e560961c3158b16c4df84f72 to your computer and use it in GitHub Desktop.
Codility - Binary Gap
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 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 |
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
import re | |
def solution(N): | |
if N < 0: return 0 | |
return len(max(re.sub(r'0+$', "", bin(N)[2:]).split('1'))) |
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 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