Problem p02975 (python)

Fixed submission:
#!/usr/bin/env python
 
import sys
 
n= int(sys.stdin.readline())
ai = [ int(x) for x in sys.stdin.readline().split() ]

a = list(set(ai))
na= len(a)
ans= "No"
if na==1 and a[0]==0: ans="Yes"
elif na==2 and 0 in a:
    x= a[0]
    if x==0: x= a[1]
    if ai.count(x)==2*ai.count(0): ans= "Yes"
elif na==3:
    x,y,z= a
    if x^y^z: pass
    else:
        n,l,m= [ ai.count(i) for i in a ]
        if n==l==m: ans="Yes"
#
print( ans )

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python
 
import sys
 
n= int(sys.stdin.readline())
ai  = [ int(x) for x in sys.stdin.readline().split() ]

mm = dict()
for i,x in enumerate(ai):
    mm[x]= mm.get(x,0) + 1
#
ans= "No"
even= list(filter(lambda i:i % 2 ==0, ai))
odd = len(list(filter(lambda i:i % 2 ==1, ai)))
f= ai.index(max(even))
ll = len(bin(ai[f]))-2
second= []
for i in range(n):
    if i==f: continue
    a= ai[i]
    if len(bin(a))-2!=ll: continue
    if odd:
        if a%2==1: second.append(a)
    else:
        if a%2==0: second.append(a)
#
for s in second:
    tmp= [ai[f],s]
    cnt= { ai[f]:1 }
    cnt[s]= cnt.get(s,0)+1
    for _ in range(2,n):
        nn = tmp[-1] ^ tmp[-2]
        cnt[nn]= cnt.get(nn,0) + 1
        if cnt[nn]>mm.get(nn,0): break
        tmp.append(nn)
    #
    if len(tmp)==n:
        x,y,z,w = tmp[-2],tmp[-1],tmp[0],tmp[1]
        if x==y^z and y==z^w:
            ans="Yes"
            break
#
print( ans )

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python
 
import sys
 
n= int(sys.stdin.readline())
ai  = [ int(x) for x in sys.stdin.readline().split() ]

mm = dict()
for i,x in enumerate(ai):
    mm[x]= mm.get(x,[])
    mm[x].append( i )
#
ans= "No"
even= list(filter(lambda i:i % 2 ==0, ai))
odd = len(list(filter(lambda i:i % 2 ==1, ai)))
f= ai.index(max(even))
ll = len(bin(ai[f]))-2
second= []
for i in range(n):
    if i==f: continue
    a= ai[i]
    if len(bin(a))-2!=ll: continue
    if odd:
        if a%2==1: second.append(i)
    else:
        if a%2==0: second.append(i)
#
for s in second:
    tmp= [f,s]
    for _ in range(2,n):
        nn = ai[tmp[-1]] ^ ai[tmp[-2]]
        i= -1
        for ii in mm.get(nn,[]):
            if not ii in tmp:
                i= ii
                break
        #
        if i>=0: tmp.append(i)
        else: break
    #
    if len(tmp)==n:
        x,y,z,w = ai[tmp[-2]],ai[tmp[-1]],ai[tmp[0]],ai[tmp[1]]
        if x==y^z and y==z^w:
            ans="Yes"
            break
#
print( ans )

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python

import sys


def is_odd(x):
    return x%2==1

n= int(sys.stdin.readline())
ai  = [ int(x) for x in sys.stdin.readline().split() ]
#
ans= "No"
even= list(filter(lambda i:i % 2 ==0, ai))
odd = len(list(filter(lambda i:i % 2 ==1, ai)))
f= ai.index(max(even))
ll = len(bin(ai[f]))-2
second= []
for i in range(n):
    if i==f: continue
    a= ai[i]
    if len(bin(a))-2!=ll: continue
    if odd:
        if a%2==1: second.append(i)
    else:
        if a%2==0: second.append(i)
#
for s in second:
    tmp= [f,s]
    for i in range(2,n):
        nn = ai[tmp[-1]] ^ ai[tmp[-2]]
        i= 0
        while True:
            try:
                i= ai.index(nn,i)
                if i in tmp: i+=1
                else: break
            except:
                i= -1
                break
        if i==-1: break
        if nn in ai and not i in tmp:
            tmp.append(i)
        else:
            break
    #
    if len(tmp)==n:
        x,y,z,w = ai[tmp[-2]],ai[tmp[-1]],ai[tmp[0]],ai[tmp[1]]
        if x==y^z and y==z^w:
            ans="Yes"
            break
#
print( ans )