Problem p02641 (python)

Fixed submission:
import random

X,N = input().split()
X,N = int(X),int(N)

P = input().split()
P = [int(c) for c in P]

if len(P) == 0:
    print(X)    
else:
    L = list(range(max(0,X-N),min(102,X+N)))

    if X+N <= 101:
        L.append(X+N)

    for i in range(N):
        if P[i] in L:
            L.remove(P[i])

    while len(L) > 2:
        rand = random.randint(1,len(L)-2)
        if L[rand] == X:
            break
        elif L[rand] > X:
            L = L[0:rand+1]
        else:
            L = L[rand:len(L)]

    if X in L:
        print(X)
    elif len(L) == 1:
        print(L[0])
    else:
        if abs(L[0]-X) > abs(L[1]-X):
            print(L[1])
        else:
            print(L[0])

Buggy submission (verdict: Runtime Error):
import random

X,N = input().split()
X,N = int(X),int(N)

P = input().split()
P = [int(c) for c in P]

if len(P) == 0:
    print(X)    
else:
    L = list(range(max(0,X-N),min(101,X+N)))

    if X+N <= 100:
        L.append(X+N)

    for i in range(N):
        if P[i] in L:
            L.remove(P[i])

    #print(L)

    while len(L) > 2:
        rand = random.randint(1,len(L)-2)
        if L[rand] == X:
            break
        elif L[rand] > X:
            L = L[0:rand+1]
        else:
            L = L[rand:len(L)]

    if X in L:
        print(X)
    else:
        if abs(L[0]-X) > abs(L[1]-X):
            print(L[1])
        else:
            print(L[0])

Buggy submission (verdict: Runtime Error):
import random

X,N = input().split()
X,N = int(X),int(N)

P = input().split()
P = [int(c) for c in P]

if len(P) == 0:
    print(X)    
else:
    L = list(range(max(0,X-N),min(101,X+N)))

    if X+N <= 100:
        L.append(X+N)

    for i in range(N):
        if P[i] in L:
            L.remove(P[i])

    #print(L)

    while len(L) > 2:
        rand = random.randint(1,len(L)-2)
        if L[rand] == X:
            break
        elif L[rand] > X:
            L = L[0:rand+1]
        else:
            L = L[rand:len(P)]

    if X in L:
        print(X)
    else:
        if abs(L[0]-X) > abs(L[1]-X):
            print(L[1])
        else:
            print(L[0])

Buggy submission (verdict: Runtime Error):
import random

X,N = input().split()
X,N = int(X),int(N)

P = input().split()
P = [int(c) for c in P]

if len(P) == 0:
    print(X)
else:
    L = list(range(max(0,X-N),min(101,X+N)))
    if X+N == 100:
        L.append(100)

    for i in range(N):
        if P[i] in L:
            L.remove(P[i])

    #print(L)

    while len(L) > 2:
        rand = random.randint(1,len(L)-2)
        if L[rand] == X:
            break
        elif L[rand] > X:
            L = L[0:rand+1]
        else:
            L = L[rand:len(P)]

    if X in L:
        print(X)
    else:
        if abs(L[0]-X) > abs(L[1]-X):
            print(L[1])
        else:
            print(L[0])

Buggy submission (verdict: Runtime Error):
import random

X,N = input().split()
X,N = int(X),int(N)

P = input().split()
P = [int(c) for c in P]

if len(P) == 0:
    print(X)
else:
    L = list(range(max(0,X-N),min(101,X+N)))

    for i in range(N):
        if P[i] in L:
            L.remove(P[i])

    while len(L) > 2:
        rand = random.randint(1,len(L)-2)
        if L[rand] == X:
            break
        elif L[rand] > X:
            L = L[0:rand+1]
        else:
            L = L[rand:len(P)]

    if X in L:
        print(X)
    else:
        if abs(L[0]-X) > abs(L[1]-X):
            print(L[1])
        else:
            print(L[0])

Buggy submission (verdict: Runtime Error):
import random

X,N = input().split()
X,N = int(X),int(N)

P = input().split()
P = [int(c) for c in P]

if len(P) == 0:
    print(X)
else:
    L = list(range(max(0,X-N),min(100,X+N)))

    for i in range(N):
        if P[i] in L:
            L.remove(P[i])

    while len(L) > 2:
        rand = random.randint(1,len(L)-2)
        if L[rand] == X:
            break
        elif L[rand] > X:
            L = L[0:rand+1]
        else:
            L = L[rand:len(P)]

    if L[rand] == X:
        print(0)
    else:
        if abs(L[0]-X) > abs(L[1]-X):
            print(L[1])
        else:
            print(L[0])