Problem p03286 (python)

Fixed submission:
import math
import sys

n = int(input())
if n == 0:
  print(0)
  sys.exit()
  
keta = 0
under = 0
s = ""
nn = 1

if n > 0:
    while True:
      if sum([2**i for i in range(0,nn,2)])+1<= n and n <= sum([2**i for i in range(0,nn+1,2)]):
          keta = nn+1
          under = sum([2**i for i in range(0,nn,2)])
          break
      else:
          nn+=1

    for i in range(1,keta):
        remainder = (n-under)%(2**i)
        if remainder in range(1,2**(i-1)+1):
            s += "0" if i%2==1 else "1"
        else:
            s += "1" if i%2==1 else "0"
    else:
        s+="1"
    print(s[::-1])

else:
    while True:
        if n <= sum([(-2)**i for i in range(1,nn,2)]) and n >= sum([(-2)**i for i in range(1,nn+1,2)]):
            keta = nn+1
            under = sum([(-2)**i for i in range(1,nn,2)])
            break
        else:
            nn+=1

    for i in range(1,keta):
        remainder = abs(n-under)%(2**i)
        if remainder in range(1,2**(i-1)+1):
            s += "1" if i%2==1 else "0"
        else:
            s += "0" if i%2==1 else "1"
    else:
        s+="1"
    print(s[::-1])

Buggy submission (verdict: Runtime Error):
import math

n = int(input())
nn = math.floor(math.log2(abs(n)))
keta = 0
under = 0
s = ""

if n > 0:
    if n <= sum([2**i for i in range(0,nn+1,2)]):
        keta = nn+1
        under = sum([2**i for i in range(0,nn,2)])
    else:
        keta = nn+3
        under = sum([2**i for i in range(0,nn+1,2)])

    for i in range(1,keta):
        remainder = (n-under)%(2**i)
        if remainder in range(1,2**(i-1)+1):
            s += "0" if i%2==1 else "1"
        else:
            s += "1" if i%2==1 else "0"
    else:
        s+="1"
    print(s[::-1])

elif n < 0:
    if n >= sum([(-2)**i for i in range(1,nn+1,2)]):
        keta = nn+1
        under = sum([(-2)**i for i in range(1,nn-1,2)])
    else:
        keta = nn+3
        under = sum([(-2)**i for i in range(1,nn+1,2)])
    for i in range(1,keta):
        remainder = abs(n-under)%(2**i)
        if remainder in range(1,2**(i-1)+1):
            s += "1" if i%2==1 else "0"
        else:
            s += "0" if i%2==1 else "1"
    else:
        s+="1"
    print(s[::-1])

else:
    print(0)

Buggy submission (verdict: Runtime Error):
import math

n = int(input())
nn = math.floor(math.log2(abs(n)))
keta = 0
under = 0
s = ""

if n > 0:
    if n <= sum([2**i for i in range(0,nn+1,2)]):
        keta = nn+1
        under = sum([2**i for i in range(0,nn,2)])
    else:
        keta = nn+3
        under = sum([2**i for i in range(0,nn+1,2)])

    for i in range(1,keta):
        remainder = (n-under)%(2**i)
        if remainder in range(1,2**(i-1)+1):
            s += "0" if i%2==1 else "1"
        else:
            s += "1" if i%2==1 else "0"
    else:
        s+="1"
    print(s[::-1])

elif n < 0:
    if n >= sum([(-2)**i for i in range(1,nn+1,2)]):
        keta = 2*(nn)
        under = sum([(-2)**i for i in range(1,nn-1,2)])
    else:
        keta = 2*(nn+1)
        under = sum([(-2)**i for i in range(1,nn+1,2)])
    for i in range(1,keta):
        remainder = abs(n-under)%(2**i)
        if remainder in range(1,2**(i-1)+1):
            s += "1" if i%2==1 else "0"
        else:
            s += "0" if i%2==1 else "1"
    else:
        s+="1"
    print(s[::-1])

else:
    print(0)

Buggy submission (verdict: Runtime Error):
n = int(input())
for i in range(2**n):
    a = list(format(i,'b'))[::-1]
    
    summ = 0
    for index,aa in enumerate(a):
        summ += int(aa)*((-2)**index)

    if summ == n:
        print(format(i,'b'))
        break