Answers for "Python BinaryGap - binary gap within a positive integer N"

0

Python BinaryGap - binary gap within a positive integer N

# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")

def solution(N):
    # write your code in Python 3.6
    #Converts N to a string with binary eq of int N
    decToBinary = bin(N).replace("0b", "")
    answer = 0
    counter = 0
    for i in decToBinary:
        if i == '1':
            if answer < counter:
                answer = counter
            counter = 0
        else:
            counter += 1
    return answer

    """
    Loops string with binary eq of N
        if it finds 1
            check if current max < counter
                update it if it is
            reset counter
            update counter
        return counter

    """
Posted by: Guest on July-08-2021
0

Python BinaryGap - binary gap within a positive integer N

# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")

def solution(N):
    # write your code in Python 3.6
    #Converts N to a string with binary eq of int N
    decToBinary = bin(N).replace("0b", "")
    answer = 0
    counter = 0
    gapExistence = False
    for i in decToBinary:
        if i == '1':
            if answer < counter:
                answer = counter
            counter = 0
        else:
            counter += 1
    return answer

    """
    Loops string with binary eq of N
        if it finds 1
            check if current max < counter
                update it if it is
                reset counter
            update counter
        return counter

    """
Posted by: Guest on July-08-2021

Code answers related to "Python BinaryGap - binary gap within a positive integer N"

Browse Popular Code Answers by Language