June Cook Off :ANKPAREN - NZEC in Python For

c-plus-plus
nzec
python27

#1

What causes NZEC in python
I submitted a code in python , i tested in Codechef IDE which gave appropriate output
when i submitted the same for the problem it gave me an NZEC

When i just wrote the same algo code in C++ 4.92 it gave me AC

Here is the link

Python : http://www.codechef.com/viewsolution/7261753

C++ : http://www.codechef.com/viewsolution/7262326


#2

Hi @geek_geek,

Only place i can see the posibility of error is using of del on the list on lines 38 and 43.

So try like this,

# your code goes here
def isval(s):
    xy = 0
    for i in s:
        if(i=='('):
            xy+=1
        else:
            xy-=1
        if(xy<0):
            return -1
    if(xy==0):
        return 1
    else:
        return -1
def prog():
    s = raw_input()
    #print "##",s
    k = input()
    if(isval(s)==-1):
        if(k==1):
            print s
        else:
            print -1
    else:
        nu = 0
        l=len(s)
        s=list(s)
        wan = []
        for i in xrange(l-1):
            if(s*=='(' and s[i+1]==')'):
                nu+=2
                wan+=[(i,i+1)]
        if(k>nu):
            print -1
        elif(k>(nu/2)):
            k-=(nu/2)
            k-=1
            x=wan[-1::-1][k][0]
            del s[x]
            print "".join(s)
        else:
            k-=1
            x=wan[k][1]
            del s[x]
            print "".join(s)
t = input()
for _ in xrange(t):
	try:
		prog()
	except Exception:
		pass

If the solution doesn’t give NZEC, proramatically it wouldn’t throw NZEC, then we can focus on ‘del’ part try to handle it in different way if length of list greater than w then delete that element.


#3

Apparently input data of the problem is not according to given format.
I too had a similar problem.

PS. I didn’t read your code, there may be other errors.


#4

Handling del in a different way doesnt help !! i think it has to be with something else