I’m trying to solve the question “Uncle Johny”. Seems pretty easy, but for some reason I’m getting a runtimeError NZEC.
This is the code. Can anyone help me with? I really don’t undestand how it doesn’t have a zero exit
t = int(input())
for _ in range(t):
n = int(input())
A = list(map(int, input().split(' ')))
u = int(input())
value = A[u - 1]
A.sort()
p = A.index(value) + 1
print(p)
okay. In your code you declared A as: A = list(map(int, input().split('')))
if you get NZEC again then you should write int(A.index(value) instead of A.index(value)
You can see my solution which worked perfectly:
# cook your dish here
s = []
t=int(input())
try :
while t>0:
t=t-1
k=int(input())
s = list(map(int,input().split()))
p = int(input())
val=s[p-1]
s.sort()
p = 1+ int(s.index(val))
print(p)
except:
pass
I apreciate your help, however, I believe I don’t have an extra bracket.
A = list(map(int, input().split(' ')))
s = list(map(int, input().split()))
The first line is my code, the second is yours. Both have 3 brackets in the end, not 2. Also, when I tried to make it 2 brackets, my code didn’t run on my IDE
I’m sorry, but I’m not proficient enough in python to help you with that, I normally use c++.
But as for whitespaces, try the 2 test cases, the first one is the example input, and the second one is also the example input, but without whitespaces.
3
4
1 3 4 2
2
5
1 2 3 9 4
5
5
1 2 3 9 4
1
3
4
1 3 4 2
2
5
1 2 3 9 4
5
5
1 2 3 9 4
1
You will get a nzec error on the input because the third array ends with a whitespace. Use the arrow key from left to right on the third array for both and you’ll see what i mean.
Ok i figured it out, look at the code if you need help, but you won’t have to do this in the newer sums
t = int(input())
for _ in range(t):
n = int(input())
seq=[]
num=""
A = input()
for i in A:
if(i!=' '):
num+=i
else:
seq.append(int(num))
num=""
if(num!=""):
seq.append(int(num))
u = int(input())
value = seq[u - 1]
seq.sort()
p = seq.index(value) + 1
print(p)
This works, because by default it only checks numbers, whereas in the other one, it checks after every whitespace, but you can bound it, by using the second parameter.
Also, .split() instead of split(’ ') works, too, since the default paramer splits using any white spaces(space, newline, etc). I didn’t found it, the other guy below your answer did.