Problem p03286 (python)

Fixed submission:
N=int(input())
if N==0:
    N=0
elif N>0:
    n=format(N,"b")
    for i in range(10**9):
        if i==len(n):
            break
        elif i%2==1:
            if n[-i-1]=="1":
                N+=2**(i+1)
                n=format(N,"b")
else:
    N*=-1
    n=format(N,"b")
    for i in range(10**9):
        if i==len(n):
            break
        elif i%2==0:
            if n[-i-1]=="1":
                N+=2**(i+1)
                n=format(N,"b")
print(int(format(N,"b")))

Buggy submission (verdict: Runtime Error):
N=int(input())
if N==0:
    a=0
elif N>0:
    a=0
    i=31
    while i>=0:
        if N%(2**i)==0:
            if i%2==1:
                a+=2**(i+1)+2**i
                break
            else:
                a+=2**i
                break
        else:
            if N>2**i:
                N-=2**i
                if i%2==1:
                    a+=2**(i+1)+2**i
                else:
                    a+=2**i
            i-=1
else:
    a=0
    i=31
    while i>=0:
        if N%(2**i)==0:
            if i%2!=1:
                a+=2**(i+1)+2**i
                break
            else:
                a+=2**i
                break
        else:
            if -N>2**i:
                N+=2**i
                if i%2!=1:
                    a+=2**(i+1)+2**i
                else:
                    a+=2**i
            i-=1
print(int(format(a,"b"))

Buggy submission (verdict: Runtime Error):
N=int(input())
if N==0:
    print(0)
elif N>0:
    a=0
    i=31
    while i>=0:
        if N%(2**i)==0:
            if i%2==1:
                a+=2**(i+1)+2**i
                break
            else:
                a+=2**i
                break
        else:
            if N>2**i:
                N-=2**i
                if i%2==1:
                    a+=2**(i+1)+2**i
                else:
                    a+=2**i
            i-=1
    print(int(str(a),2))
else:
    a=0
    i=31
    while i>=0:
        if N%(2**i)==0:
            if i%2!=1:
                a+=2**(i+1)+2**i
                break
            else:
                a+=2**i
                break
        else:
            if -N>2**i:
                N+=2**i
                if i%2!=1:
                    a+=2**(i+1)+2**i
                else:
                    a+=2**i
            i-=1
    print(int(str(a),2))

Buggy submission (verdict: Runtime Error):
N=int(input())
if N==0:
    print(0)
elif N>0:
    a=0
    i=31
    while i>=0:
        if N%(2**i)==0:
            if i%2==1:
                a+=2**(i+1)+2**i
                break
            else:
                a+=2**i
                break
        else:
            if N>2**i:
                N-=2**i
                if i%2==1:
                    a+=2**(i+1)+2**i
                else:
                    a+=2**i
            i-=1
    print(int(format(a,2)))
else:
    a=0
    i=31
    while i>=0:
        if N%(2**i)==0:
            if i%2!=1:
                a+=2**(i+1)+2**i
                break
            else:
                a+=2**i
                break
        else:
            if -N>2**i:
                N+=2**i
                if i%2!=1:
                    a+=2**(i+1)+2**i
                else:
                    a+=2**i
            i-=1
    print(int(format(a,2)))