Write it
Rank the scores
A functionPython or Java11 tests, 8 of them hidden
On a phone? Put the lines in order instead
An online puzzle game keeps the scores of its players in a list (an array in Java) called scores. The leaderboard shows them from highest to lowest.
Write a function that uses a selection sort to put the scores in order from highest to lowest, and returns the sorted list.
- On each pass, find the position of the highest score in the part of the list not yet sorted, then swap it with the first score of that part.
- Equal scores stay in the list as many times as they appeared.
scorescan be empty. Scores are whole numbers between -1000 and 100000 (a score can be below 0 after penalties).- You may change
scoresand return it, or return a new list. - Write the sort yourself. The shortcuts listed with this question are not allowed.
The tests check only the list your function returns, so they cannot tell a selection sort from another sort. In the exam, a different sort would not earn the marks for this part.
def rank_scores(scores: list[int]) -> list[int]
You cannot use these in this question:
- sorted()
- max()
- min()
- reversed()
- .sort()
- .reverse()
- .index()
- import heapq
- import bisect
Paper 2 asks you to write a sorting algorithm yourself, so the built-in sorts and the shortcuts for finding the largest value are not allowed.
Examples
Your function is tested on 11 cases: the 3 examples below, and 8 hidden ones that check the rules above.
- rank_scores([30, 90, 10, 60]) should return [90, 60, 30, 10]: four different scores
- rank_scores([50, 20, 50, 70]) should return [70, 50, 50, 20]: two equal scores both stay (rule 2)
- rank_scores([5, 9, 1, 12]) should return [12, 9, 5, 1]: the highest score is last, so the search must reach the end of the list
public static int[] rankScores(int[] scores)
Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.Checking whether Java answers can run here.
You cannot use these in this question:
- Arrays.sort()
- Arrays.parallelSort()
- Collections.sort()
- Collections.reverse()
- Collections.max()
- Collections.min()
- Math.max()
- Math.min()
- .sort()
- .stream()
- .sorted()
- .max()
- .min()
- import java.util.stream
Paper 2 asks you to write a sorting algorithm yourself, so the built-in sorts and the shortcuts for finding the largest value are not allowed.
Examples
Your function is tested on 11 cases: the 3 examples below, and 8 hidden ones that check the rules above.
- rankScores(new int[]{30, 90, 10, 60}) should return [90, 60, 30, 10]: four different scores
- rankScores(new int[]{50, 20, 50, 70}) should return [70, 50, 50, 20]: two equal scores both stay (rule 2)
- rankScores(new int[]{5, 9, 1, 12}) should return [12, 9, 5, 1]: the highest score is last, so the search must reach the end of the list