Getting wrong answer on submission but test cases are running fine (ZCO12001)

problem code: ZCO12001

try:
n=int(input())
l=list(map(int,input().split()))
maxlength=0
l1=[]
length=0
c=0
maxc=0
pos1=0
pos2=0
maxpos1=0
maxpos2=0
for i in range(0,n):

    if(l[i]==1):
        if(len(l1)==0):
            l1.append(l[i])
            c=1
            length=1
            pos1=i+1
            pos2=i+1
 
            if(length>maxlength):
                maxlength=length
                maxpos2=pos2
            if(c>maxc):
                maxc=c
                maxpos1=pos1   
        elif(len(l1)!=0):
            l1.append(l[i])
            c+=1
            pos1+=1
            if(c>maxc):
                maxc=c
                maxpos1=pos1
            length+=1
            if(length>maxlength):
                maxlength=length
                maxpos2=pos2
    elif(l[i]==2):
        c-=1
        length+=1
        l1.pop()
        if maxlength<length:
            maxlength=length
            maxpos2=pos2
print(maxc,end=" ")
print(maxpos1,end=" ")
print(maxlength,end=" ")
print(maxpos2,end=" ")

except:
pass

As you are using try except, you won’t know whether it is wrong answer or run time error.
Try block catches the error and starts running from except block. So, till then whatever is printed(even if nothing is printed), that wont match the expected output.

2 Likes

If I ruled the world, everyone who recommended using tryexcept blocks to “fix” runtime errors in Python would be banned from using their computer for a month :slight_smile:

Edit:

Please Like @vasu_vg’s post above rather than mine, as he actually explains why it’s bad :slight_smile:

Edit2:

Oh, great - now I’ve bumped this ancient thread to the top XD Soz!

10 Likes