Skip to content

Instantly share code, notes, and snippets.

@sehajyang
Created August 27, 2019 07:15
Show Gist options
  • Save sehajyang/46af174181d88a7349384bb60f45b18d to your computer and use it in GitHub Desktop.
Save sehajyang/46af174181d88a7349384bb60f45b18d to your computer and use it in GitHub Desktop.
codility binarygap by python
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