Coding Quiz - Sorting Tuples by Score
In this coding quiz, you are tasked with writing a function that sorts a list of tuples consisting of names and scores in descending order by the score.
For example, in the input list [('Tom', 70), ('Jane', 88), ('Max', 95), ('Alice', 85)]
, the first tuple's Tom
represents a name, and 70
represents a score.
Sort this list in descending order by the score, resulting in [('Max', 95), ('Jane', 88), ('Alice', 85), ('Tom', 70)]
.
def solution(scores): # Write your code here return
Constraints
-
The list contains at least one tuple.
-
Each tuple consists of a string and an integer, where the string represents a person's name and the integer represents the score.
Example
-
Input: [('Tom', 70), ('Jane', 88), ('Max', 95), ('Alice', 85)]
-
Output: [('Max', 95), ('Jane', 88), ('Alice', 85), ('Tom', 70)]
Lecture
AI Tutor
Publish
Design
Upload
Notes
Favorites
Help