Created
August 27, 2019 07:15
-
-
Save sehajyang/46af174181d88a7349384bb60f45b18d to your computer and use it in GitHub Desktop.
codility binarygap by python
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): | |
_gap = 0 | |
prev = 0 | |
binary_list = [int(x) for x in bin(N)[2:]] | |
for idx, value in enumerate(binary_list): | |
if idx == len(binary_list) - 1: | |
if _gap == 0: | |
return 0 | |
else: | |
return _gap - 1 | |
else: | |
if value == 1: | |
if idx - prev > _gap: | |
_gap = idx - prev | |
prev = idx |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment