Home>

Find the largest value in the list and the index of the last element that has the given value in one pass through the list, without modifying this list or using an additional list.

My code:

a= input (). split ()
print (max (a), a.index (max (a)))

There is a problem where two or more elements in a list are equal to the maximum of the list. How do I get the index of the latter?

you already have two passes

splash582021-10-13 20:31:58

@ splash58 I'll fix it to a variable as soon as I figure out how to get the maximum index

Nick Proskuryakov2021-10-13 20:31:58

don't you think that a regular loop will solve the problem easier?

splash582021-10-13 20:31:58

len (a) -a [:: -1] .index (max (a) -1

splash582021-10-13 20:31:58

but you have one pass to max, the second to flip the sheet, the third to find the index

splash582021-10-13 20:31:58