Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

binary gap python

def solution(N):
    my_binary = format(N, "b")
    my_list = list(my_binary)
    keeper = []
    count = 0
    print(my_binary)
    for x in range(len(my_list)):
        if my_list[x] == '1':
            if x == len(my_list) - 1 or my_list[x + 1] == '1':
                pass
            else:
                keeper.append(changer(x, my_list))

        for q in keeper:
            if q == 0:
                pass
            elif count < (q[1] - q[0]):
                count = q[1] - q[0]
    return count


def changer(x, my_list):
    for y in range(x, len(my_list)):
        if my_list[y] == '0' and y != len(my_list) - 1 and my_list[y + 1] == '1':
            return [x, y]
    return 0

print(solution(11))
 
PREVIOUS NEXT
Tagged: #binary #gap #python
ADD COMMENT
Topic
Name
2+8 =