Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Say i got an ArrayList with the following values: [31,41,46,2,61,71,83,2,52,2,28

ID: 3649741 • Letter: S

Question

Say i got an ArrayList with the following values:


[31,41,46,2,61,71,83,2,52,2,28] //11 int values total

0 1 2 3 4 5 6 7 8 9 10 //indexes


I need help writing a method that finds the smallest value and returns its position. If the values repeat however, It must return the position towards the end.


So in the case above, 2 is the smallest value but it repeats at index 3, index 7 and index 9. The method should return index 9.


[31,41,46,2,61,71,83,1,52,2,28] //11 int values total

0 1 2 3 4 5 6 7 8 9 10 //indexes


Where as this case, the smallest value is 1, so it should return index 7.


The method must be generic enough as the number of repeated occurences for the min values is unknown. the arraylist could easily be the same number so the min would be that number and the index returned must be 10.


like so:


[2,2,2,2,2,2,2,2,2,2,2]



Explanation / Answer

Since you already know how to find the minimum value, you simply feed that value to the index() function to get the index of this value in the list. I.e, n = [20, 15, 27, 30] n.index(min(n)) yields 1 This will return the index of the minimum value in the list. Note that if there are several minima it will return the first. min(): With a single argument iterable, return the smallest item of a non-empty iterable (such as a string, tuple or list). With more than one argument, return the smallest of the arguments. list.index(x): Return the index in the list of the first item whose value is x. It is an error if there is no such item.