Turning a while loop to a recursion

I'm trying to turn this code ( that gives the maximum number of consecutive 0's in a binary) to a recursive code. I tried in many ways but the result is wrong. This is the code using a while loop :

def consecutivezeros(N):
    if N==0 :
        return 1
    # variable to store the length
    # of longest consecutive 0's
    maxm = -1

    # to temporary store the
    # consecutive 0's
    cnt = 0
    while (N):
        if (not (N & 1)):
            cnt += 1
            N >>= 1
            maxm = max(maxm, cnt)
        else:
            maxm = max(maxm, cnt)
            cnt = 0
            N >>= 1

    return maxm


Read more here: https://stackoverflow.com/questions/66330489/turning-a-while-loop-to-a-recursion

Content Attribution

This content was originally published by kr4t0s26 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: