Problem p03209 (python)

Fixed submission:
#!/mnt/c/Users/moiki/bash/env/bin/python
N, X = map(int, input().split())

    
L = [1]
P = [1]

for i in range(0, N+1):
    L.append( L[i] * 2 + 3)
    P.append( P[i] * 2 + 1)

        
    
def calc_pan(n,x):
    # c = judge(n,x)
    if n == 0:
        return 0 if x <= 0 else 1
    elif x <= 1 + L[n-1]:
        return calc_pan(n-1, x-1)
    else:
        return P[n-1] + 1  + calc_pan(n-1, x - 2 - L[n-1])

sum = calc_pan(N, X)
print(sum)

Buggy submission (verdict: Runtime Error):
#!/mnt/c/Users/moiki/bash/env/bin/python
N, X = map(int, input().split())

def calcl(n):
    l = 2 ** (n+2) - 3 
    return l
def calcp(n):
    p = 2 ** (n+1) - 1
    return p
    
L = [1]
P = [1]

for i in range(1, N+1):
    L.append( L[i-1] * 2 + 3)
    P.append( P[i-1] * 2 + 1)

def judge(n, x):
    # if n == 0:
    #     if x == 1:
    #         return 1
    #     else:
    #         raise "error"
    if x == 1:
        if n == 0:
            return "3"
        else:
            return "2"
    else:
        l = L[n]       # layers
        top = l
        bottom = 1
        mid = (l-1)//2 # index of middle

        if x == mid:
            return "3"
        elif top > x > mid:
            return "1"
        elif 0 < x < mid:
            return "0"            
        elif x == top:
            return "4"
        elif x == bottom:
            return "2"
        else:
            raise "No such pattern!"
        
    
def calc_pan(n,x):
    c = judge(n,x)
    # print("calc_panK: n:{}, x:{}, c: {}".format(n,x,c))
    if  c == "2":
        return 0
    elif c == "4":
        return 1 + P[n-1] * 2
    elif c == "3":
        return 1 + P[n-1]
    elif c == "0":
        return calc_pan(n-1, x-1)
    elif c == "1":
        return P[n-1] + 1 + calc_pan(n-1, x-L[n-1]-2)





sum = calc_pan(N, X)
print(sum)

Buggy submission (verdict: Runtime Error):
#!/mnt/c/Users/moiki/bash/env/bin/python
N, X = map(int, input().split())

def calcl(n):
    l = 2 ** (n+2) - 3 
    return l
def calcp(n):
    p = 2 ** (n+1) - 1
    return p
    
L = [1]
P = [1]

for i in range(1, N+1):
    L.append( L[i-1] * 2 + 3)
    P.append( P[i-1] * 2 + 1)

def judge(n, x):
    # if n == 0:
    #     if x == 1:
    #         return 1
    #     else:
    #         raise "error"
    if x == 1:
        if n == 0:
            return "3"
        else:
            return "2"
    else:
        l = L[n]       # layers
        top = l-1
        bottom = 0
        mid = (l-1)//2 # index of middle

        if x == mid:
            return "3"
        elif top > x > mid:
            return "1"
        elif 0 < x < mid:
            return "0"            
        elif x == top:
            return "4"
        elif x == bottom:
            return "2"
        else:
            raise "No such pattern!"
        
    
def calc_pan(n,x):
    c = judge(n,x)
    # print("calc_panK: n:{}, x:{}, c: {}".format(n,x,c))
    if  c == "2":
        return 0
    elif c == "4":
        return 1 + P[n-1] * 2
    elif c == "3":
        return 1 + P[n-1]
    elif c == "0":
        return calc_pan(n-1, x-1)
    elif c == "1":
        return P[n-1] + 1 + calc_pan(n-1, x-L[n-1]-2)





sum = calc_pan(N, X)
print(sum)

Buggy submission (verdict: Runtime Error):
#!/mnt/c/Users/moiki/bash/env/bin/python
N, X = map(int, input().split())

def calcl(n):
    l = 2 ** (n+2) - 3 
    return l
def calcp(n):
    p = 2 ** (n+1) - 1
    return p
    
L = [1]
P = [1]

for i in range(1, N+1):
    L.append( L[i-1] * 2 + 3)
    P.append( P[i-1] * 2 + 1)

def judge(n, x):
    if n == 0:
        if x == 1:
            return 1
        else:
            raise "error"
    # if x == 1:
    #     if n == 0:
    #         return "3"
    #     else:
    #         return "2"
    else:
        l = L[n]       # layers
        top = l-1
        bottom = 0
        mid = (l-1)//2 # index of middle

        if x == mid:
            return "3"
        elif top > x > mid:
            return "1"
        elif 0 < x < mid:
            return "0"            
        elif x == top:
            return "4"
        elif x == bottom:
            return "2"
        else:
            raise "No such pattern!"
        
    
def calc_pan(n,x, sum):
    c = judge(n,x)
    # print("calc_panK: n:{}, x:{}, c: {}".format(n,x,c))
    if  c == "2":
        return sum
    elif c == "4":
        return sum + 1 + P[n-1]
    elif c == "3":
        return sum + 1 
    elif c == "0":
        return sum + calc_pan(n-1, x-1, 0)
    elif c == "1":
        return sum + P[n-1] + 1 + calc_pan(n-1, x-L[n-1]-2, 0)





sum = calc_pan(N, X, 0)
print(sum)

Buggy submission (verdict: Runtime Error):
#!/mnt/c/Users/moiki/bash/env/bin/python
N, X = map(int, input().split())

def calcl(n):
    l = 2 ** (n+2) - 3 
    return l
def calcp(n):
    p = 2 ** (n+1) - 1
    return p
    
L = [1]
P = [1]

for i in range(1, N+1):
    L.append( L[i-1] * 2 + 3)
    P.append( P[i-1] * 2 + 1)

def judge(n, x):
    if x == 1:
        if n == 0:
            return "3"
        else:
            return "2"
    else:
        l = L[n]       # layers
        top = l-1
        bottom = 0
        mid = (l-1)//2 # index of middle

        if x == mid:
            return "3"
        elif top > x > mid:
            return "1"
        elif 0 < x < mid:
            return "0"            
        elif x == top:
            return "4"
        elif x == bottom:
            return "2"
        else:
            raise "No such pattern!"
        
    
def calc_pan(n,x, sum):
    c = judge(n,x)
    # print("calc_panK: n:{}, x:{}, c: {}".format(n,x,c))
    if  c == "2":
        return sum
    elif c == "4":
        return sum + 1 + P[n-1]
    elif c == "3":
        return sum + 1 
    elif c == "0":
        return sum + calc_pan(n-1, x-1, 0)
    elif c == "1":
        return sum + P[n-1] + 1 + calc_pan(n-1, x-L[n-1]-2, 0)





sum = calc_pan(N, X, 0)
print(sum)

Buggy submission (verdict: Runtime Error):
#!/mnt/c/Users/moiki/bash/env/bin/python
N, X = map(int, input().split())

def calcl(n):
    l = 2 ** (n+2) - 3 
    return l
def calcp(n):
    p = 2 ** (n+1) - 1
    return p
    
L = [1]
P = [1]

for i in range(1, N+1):
    L.append( L[i-1] * 2 + 3)
    P.append( P[i-1] * 2 + 1)
# for i in range(1, N+1):
#     L.append(0)
#     P.append(0)
#     L[i] = calcl(i)
#     P[i] = calcp(i)


# print("L, P: {}, {}".format(L, P ) ) 


def judge(n, x):
    if x == 1:
        if n == 0:
            return "3"
        else:
            return "2"
    else:
        l = L[n]       # layers
        top = l-1
        bottom = 0
        mid = (l-1)//2 # index of middle
        # print("l, mid: {}, {}, {}".format(l,mid,x))

        if x == mid:
            return "3"
        elif top > x > mid:
            return "1"
        elif 0 < x < mid:
            return "0"            
        elif x == top:
            return "4"
        elif x == bottom:
            return "2"
        else:
            raise "No such pattern!"
        
    
def calc_pan(n,x, sum):
    c = judge(n,x)
    # print("calc_panK: n:{}, x:{}, c: {}".format(n,x,c))
    if  c == "2":
        return sum
    elif c == "4":
        return sum + 1 + P[n-1]
    elif c == "3":
        return sum + 1 + P[n-1]
    elif c == "0":
        return sum + calc_pan(n-1, x-1, 0)
    elif c == "1":
        return sum + P[n-1] + 1 + calc_pan(n-1, x-L[n-1]-2, 0)





sum = calc_pan(N, X, 0)
print(sum)