Assigning Unique Score Ratings in a CompetitionAssigning Unique Score Ratings in a Competition

MD Jamil Kashem Porosh
3 min readSep 20, 2023

--

In competitive scenarios, it is often necessary to assign ranks or ratings to individuals based on their performance. This blog post discusses a problem where we are given an array of scores representing the performance of individuals in a competition. Our task is to assign unique rank labels to each individual based on their scores. The top three ranks are associated with specific medals, while the remaining ranks are assigned their placement numbers. We will provide a solution in both JavaScript and Python, along with an example to illustrate the approach.

Problem Statement:

Given an array of integers scores, representing the scores of individuals in a competition, our goal is to assign unique rank labels to each individual based on their scores. The first rank receives a "Golden Medal," the second rank a "Silver Medal," and the third rank a "Bronze Medal." For ranks 4 through n, the individuals are assigned their placement number as the rank.

JavaScript Implementation:

function assignRanks(scores) {
const sortedScores = [...scores].sort((a, b) => b - a);
const ranks = [];

for (let i = 0; i < scores.length; i++) {
if (i === 0) {
ranks.push("Golden Medal");
} else if (i === 1) {
ranks.push("Silver Medal");
} else if (i === 2) {
ranks.push("Bronze Medal");
} else {
ranks.push((i + 1).toString());
}
}

return ranks;
}

// Example usage
const scores = [5, 4, 3, 2, 1];
const result = assignRanks(scores);
console.log(result); // Output: ["Golden Medal", "Silver Medal", "Bronze Medal", "4", "5"]

Explanation:

  1. The function assignRanks takes the scores array as input.
  2. We create a copy of the scores array and sort it in descending order using the sort method.
  3. We initialize an empty array, ranks, to store the assigned ranks.
  4. We iterate through the scores array using a for loop and assign ranks based on the index.
  5. For the first three indices (0, 1, and 2), we assign the corresponding medal ranks.
  6. For the remaining indices, we assign the placement number by converting the index to a string using the toString method and pushing it to the ranks array.
  7. Finally, we return the ranks array.

Python Implementation:

def assign_ranks(scores):
sorted_scores = sorted(scores, reverse=True)
ranks = []

for i in range(len(scores)):
if i == 0:
ranks.append("Golden Medal")
elif i == 1:
ranks.append("Silver Medal")
elif i == 2:
ranks.append("Bronze Medal")
else:
ranks.append(str(i + 1))

return ranks

# Example usage
scores = [5, 4, 3, 2, 1]
result = assign_ranks(scores)
print(result) # Output: ["Golden Medal", "Silver Medal", "Bronze Medal", "4", "5"]

Explanation:

  1. The function assign_ranks takes the scores list as input.
  2. We create a sorted version of the scores list in descending order using the sorted function.
  3. We initialize an empty list, ranks, to store the assigned ranks.
  4. We iterate through the scores list using a for loop and assign ranks based on the index.
  5. For the first three indices (0, 1, and 2), we assign the corresponding medal ranks.
  6. For the remaining indices, we assign the placement number by converting the index to a string using the str function and appending it to the ranks list.
  7. Finally, we return the ranks list.

Conclusion:

In this blog post, we discussed the problem of assigning unique score ratings in a competition. We provided solutions in both JavaScript and Python, demonstrating how to assign ranks based on scores while associating the top three ranks with specific medals. By utilizing sorting and iteration techniques, we were able to assign the appropriate ranks to each individual in the competition.

Note::

👋 Hey there! If you have any burning questions or just want to say hi, don’t be shy — I’m only a message away. 💬 You can reach me at jamilkashem@zoho.com.

🤝 By the way, I think we share the same interest in software engineering — let’s connect on LinkedIn! 💻

--

--

MD Jamil Kashem Porosh

Software Engineer | 📝 Tech Blogger | React.js, React Native, JavaScript, Go, Python. (✉️ jamilkashem@zoho.com)