Bug in "The Lead Game" Program

What do you mean?
It seems to work fine.

please check the code in python attached…logic is perfect…I’m getting runtime error

It’s not bugged, you are unsafely using the max() function. When you are looking the max() element in “l” and “m” lists, you assume that there will at least be 1 element, which is not right.

When you force max() with an empty list or iterator, it throws a Runtime Error.

Plus, your final if condition is not right for the same reason. Why would you assume both of them have to be non empty?

You probably meant this:

t = int(input())
l = []
m = []
x,y=0,0

for i in range(t):
    a,b = map(int,input().split())
    x+=a
    y+=b
    if(x>y):
        l.append(x-y)
    else:
        m.append(y-x)

p = max(l) if len(l) > 0 else -1
q = max(m) if len(m) > 0 else -1
if(p>q):
    print(1, p)
else:
    print(2, q)

This only works under these constraints. Don’t believe this way to use max() can be used always.

1 Like

Thank you for Correction

1 Like