dimanche 4 octobre 2020

why max function does not print proper output in my code?

In the following code user enters N pair of inputs each time. When users enter value 2, the most recent tuple will be deleted and value 3 prints the max of 2nd element of remaining tuples . Assume the input list is B=[(1,4),(1,37),2,3,(1,29),3]. Therefore, 2 will remove (1,37). Then (1,4) remains on the list then 3 prints value 4 of (1,4). By entering (1,29) remaining list will be ([1,4],[1,29]) and code should print the max of 2nd element in tuples which is 29 but it prints 4 instead of 29.

So expected output is:

4
29

But my code output is:

4
4
N = int(input())
inputs = []
for i in range(N):
    inputs.append(input().split())
B = []
for b in inputs:
    if len(b) == 2:
        B.append(b)
    if b == ['2']:
        del B[-1]  
    if b == ['3']:
        print(max(B,key=lambda x:x[1])[1])

Aucun commentaire:

Enregistrer un commentaire