Relative Ranks
In this problem, we are asked to rank the scores of athletes in a competition.
Here’s how we can approach it:
Make a copy of the original list and sort it in descending order. The reason we sort a copy instead of the original list is that we need to maintain the original order of athletes as per the problem statement.
Iterate through the sorted scores and assign each score a medal or a rank. The first score gets the “Gold Medal”, the second gets the “Silver Medal”, the third gets the “Bronze Medal”, and the rest get their respective rank as a string.
Finally, we iterate through the original list of scores, and for each score, we append its rank to the result list.
Here’s a Python solution using this approach:


This solution iterates through the scores and ranks them accordingly, keeping track of the original order as required. It achieves this by using a dictionary to map scores to their ranks and then constructs the result based on the original order.


Problem Classification
The problem is in the domain of data manipulation and sorting algorithms in computer science. It involves dealing with an array of integers and modifying it to reflect the ranking based on the values.
‘What’ Components:
 An integer array,
score
, which represents the scores of athletes. Each score is unique and corresponds to a specific athlete.  The athletes are ranked based on their scores  the highest scoring athlete is ranked 1, the second highest is ranked 2, and so forth.
 Special titles are given to the top 3 ranks  1st place is “Gold Medal”, 2nd place is “Silver Medal”, and 3rd place is “Bronze Medal”.
 From the 4th rank onward, the rank is the placement number.
 The output is an array where each element is the rank of the corresponding athlete in the input array.
This is a sorting and mapping problem. It involves sorting the array in descending order to determine the ranks, mapping the top three scores to their corresponding medals, and then replacing the rest of the scores with their corresponding ranks. The challenge lies in keeping track of the original position of each score, which adds an element of index manipulation to the problem.
Language Agnostic Coding Drills
 Dissect the code and identify each distinct concept it contains:
The code makes use of several important coding concepts:
Lists and List Manipulation: Lists are fundamental data structures in Python. They are used to store multiple items in a single variable.
Enumerate: Enumerate is a builtin function of Python. It allows us to loop over something and have an automatic counter.
Heap and Heap Operations: In Python, it is available using the
heapq
module. The property of this data structure in Python is that each time the smallest heap element is popped(minheap). Whenever elements are pushed or popped, heap structure is maintained. In this code, a max heap is created by inserting a negative version of the values.Conditional Statements (IfElse): These are used to perform different actions based on different conditions.
String Formatting: It’s a way to “format” or manipulate strings.
List them out in order of increasing difficulty:
Lists and List Manipulation: This is a basic concept in Python and most other programming languages.
Conditional Statements (IfElse): This concept is also one of the first that programmers learn. It is slightly more complex than lists as it involves logic and control flow.
Enumerate: Enumerate is a somewhat more advanced concept. Understanding it requires a grasp of both lists and control flow.
String Formatting: While basic string formatting is not overly complex, advanced uses can require a significant understanding of Python’s string manipulation capabilities.
Heap and Heap Operations: This is a more advanced concept that requires understanding of both data structures and algorithms.
Problemsolving approach leading to the final solution:
The problemsolving approach involves sorting the scores in descending order using a heap data structure while keeping track of the original indices. This allows the ranks to be assigned to the correct athletes. A counter, r
, is used to keep track of the current rank, which is used to assign either a medal (for ranks 1, 2, and 3) or a numeric rank. The ranks are then added to the ans
list in the appropriate position based on the original index. This process continues until all athletes have been ranked.
Targeted Drills in Python
Separate Python code for each identified concept:
Lists and List Manipulation:
1 2 3
my_list = [1, 2, 3, 4, 5] # Creating a list my_list.append(6) # Adding an element to the list len_my_list = len(my_list) # Getting the length of the list
Conditional Statements (IfElse):
1 2 3 4 5
x = 10 if x > 5: print("x is greater than 5") else: print("x is not greater than 5")
Enumerate:
1 2 3
my_list = ['a', 'b', 'c', 'd'] for i, value in enumerate(my_list): print(f"Index: {i}, Value: {value}")
String Formatting:
1 2
x = 10 print(f"The value of x is: {x}")
Heap and Heap Operations:
1 2 3 4 5 6
import heapq heap = [] # Creating an empty heap heapq.heappush(heap, 3) # Pushing elements into heap heapq.heappush(heap, 1) heapq.heappush(heap, 2) print(heapq.heappop(heap)) # Popping the smallest element
Problemspecific concepts:
For this problem, the key problemspecific concept is keeping track of the original indices of the athletes while sorting their scores. This allows us to assign the ranks to the correct athletes. This is accomplished by storing each score along with its index in the heap.
Here’s a drill for this concept:
1 2 3 4 5 6 7 8
import heapq scores_with_indices = [] scores = [5, 3, 4, 2, 1] for i, score in enumerate(scores): heapq.heappush(scores_with_indices, (score, i)) while scores_with_indices: score, i = heapq.heappop(scores_with_indices) print(f"Score: {score}, Index: {i}")
Integration:
Once all the drills are understood, they can be integrated as follows to solve the problem:
First, create an empty list
rankings
and push the negative of the scores along with their original indices into it.Also, create an empty list
ans
of size n to store the ranks of the athletes.Then, use a counter
r
to keep track of the current rank.Start a loop that continues until
rankings
is empty. In each iteration, pop the maximum score (and its index) from the heap.If
r
is less than or equal to 3, assign the corresponding medal to the athlete (stored in theans
list at the correct position).If
r
is greater than 3, assign the numeric rank to the athlete.Finally, return the
ans
list containing the ranks of all athletes.
This will result in a solution that correctly ranks the athletes according to their scores.