Input : test_list = [3, 4, 6, 5, 3, 4, 9, 1, 2, 1, 8, 3, 2, 3, 9]
Output : [(1, 16.0), (2, 10.0), (3, 9.333333333333334), (4, 1.5), (5, 0.6), (6, 0.3333333333333333), (8, 1.25), (9, 2.2222222222222223)]
Explanation : 1 occurs in list at index 7 and 9. 9 + 7 = 16 / 1 = 16.
Input : test_list = [3, 4, 6, 5, 3, 4, 9, 1, 2, 1, 8, 3, 2, 3, 9]
Output : [(1, 16.0), (2, 10.0), (3, 9.333333333333334), (4, 1.5), (5, 0.6), (6, 0.3333333333333333), (8, 1.25), (9, 2.2222222222222223)]
Explanation : 5 occurs at index 3. 3 / 5 = 0.6.
In this, the indices of elements are extracted using filter() and list comprehension. The set() is used to get unique numbers that are present in the list. The sum() is used to compute the summation of all indices.
The original list is : [3, 4, 6, 5, 3, 4, 9, 1, 2, 1, 8, 3, 2, 3, 9]
Index rank of each element : [(1, 16.0), (2, 10.0), (3, 9.333333333333334), (4, 1.5), (5, 0.6), (6, 0.3333333333333333), (8, 1.25), (9, 2.2222222222222223)]
Initialize the input list test_list.
Print the original list.
Using set() function, get a set of unique elements in the list.
Use dictionary comprehension to get index rank of each element in the list. Inside the comprehension, the lambda function filters out indices of the current element from the list and then the sum of those indices is divided by the element to get the index rank.
Sort the dictionary by value using the sorted() function with the key parameter set to lambda function.
Print the result.
OutputThe original list is : [3, 4, 6, 5, 3, 4, 9, 1, 2, 1, 8, 3, 2, 3, 9]
Index rank of each element : [(6, 0.3333333333333333), (5, 0.6), (8, 1.25), (4, 1.5), (9, 2.2222222222222223), (3, 9.333333333333334), (2, 10.0), (1, 16.0)]
Time complexity: O(n^2), where n is the length of the input list test_list.
Auxiliary space: O(n), where n is the length of the input list test_list.