why is it wrong

link text

question


i = (input())
d=[]
for k in range(int(i)):
    a = input()
    b = a.split(" ")
    for j in range(len(b)):
        if int(b[j]) == (len(b)-1):
            b.pop(j)
            break
    d.append(int(max(b)))
for u in d:
    print(u)

this is the question
and this is my answer in python why when i submitting it is not accepting my code

1 Like

It’s giving wrong answer for this:

10 6 2 8 3 4 3 3 10 6 4

Answer should be 10 not 8. It’s happening because elements in b are strings and not int so it’s taking ‘8’ > ‘10’.

1 Like

The error is with the line:


if int(b[j]) == (len(b)-1):
    b.pop(j)

That is, since you are comparing the element j of array b, if you update the array b (by the pop function) the length changes too. Meaning, there may be another number that will be popped instead of only the value N-1.

Fix this by making a variable assigned with the initial value of len(b)-1 so that it will not change throughout the loop.

https://www.codechef.com/viewsolution/14733406

An easier way is to use remove(). That is b.remove(len(b)-1). It removes the first occurrence of the value in the list. You wouldn’t even need a loop for it! :slight_smile:

Hope it helps~!

thanks you both for helping now i have do it.


i = (input())
d=[]
for k in range(int(i)):
a = input()
b = a.split(" ")
c=[]
for j in b:
    c.append(int(j))
c.remove(len(c)-1)
d.append(int(max(c)))
for u in d:
print(u)
//