Problem overview
Given a list of integer scores from 0 to 100, return the letter grade of each score, in the same order. The ranges are 90 to 100 for "A", 80 to 89 for "B", 70 to 79 for "C", 60 to 69 for "D" and 0 to 59 for "F".
The work is split in two. letterGrade is a helper that turns one score into its letter. letterGrades goes over the list and collects the helper's answer for every score. For [95, 82, 71] the result is ["A", "B", "C"], and for [100, 90, 89] it is ["A", "A", "B"]: 90 is the lowest A, and 89 is one point short.
Approach
The helper checks the ranges from the top down. A score of at least 90 is an A. If it is not, it is below 90, so a score of at least 80 must be a B, and so on. Each check can lean on the ones before it, so it only needs a lower bound, never a pair of bounds like 80 <= score <= 89.
The main method is the usual collect-in-a-list pattern: start with an empty list, visit each score, append its letter, return the list.
Step by step
- Check for an A first: if
score >= 90, return"A". Becausereturnends the function, the checks below only ever see scores under 90. - Check for a B:
score >= 80now means 80 to 89. - Check for a C:
score >= 70means 70 to 79. - Check for a D:
score >= 60means 60 to 69. - Return
"F"after the lastif. Only scores below 60 get here, so no test and noelseis needed. - In
letterGrades, start an empty list namedgrades. - Loop over
scores, one score at a time. - Append
self.letterGrade(score)togrades. The helper's parameter is also calledscore, but each function has its own variables: the call hands the helper this loop's value. - Return
gradesafter the loop. Returning inside the loop would send back only the first letter.
The solution in Python
class Solution:
def letterGrade(self, score: int) -> str:
if score >= 90:
return "A"
if score >= 80:
return "B"
if score >= 70:
return "C"
if score >= 60:
return "D"
return "F"
def letterGrades(self, scores: List[int]) -> List[str]:
grades = []
for score in scores:
grades.append(self.letterGrade(score))
return grades
Python details:
- Separate
ifstatements work here because each one returns. There is no fall-through, soelifwould behave the same. The chain ofiflines is just as correct and easy to read. self.before the helper's name is how one method of the class calls another.appendchanges the list in place and returnsNone. Don't writegrades = grades.append(...): that replaces the list withNone.
Following one score through the checks
Take a score of 82. The test score >= 90 is false, so the function moves on. The test score >= 80 is true, so it returns "B" and the remaining tests never run. A score of 40 fails all four tests and reaches the final return "F". A score of 90 passes the very first test, which is why 90 is an A and 89 is a B: 89 fails that test and passes the next one.
Why the helper is a separate method
The range checks and the looping are two different jobs. Keeping letterGrade separate means it can be read, tested and fixed by itself: letterGrade(95) returns "A", letterGrade(82) returns "B", and letterGrade(40) returns "F". The list method then has no grading rules in it, only the loop. If the cut-offs change, only the helper changes.
The loop could also be written as a list comprehension, [self.letterGrade(s) for s in scores], which returns the same list. The explicit loop with append is the form the lesson practises: start empty, visit each item, add one result per item.
An empty list of scores would give an empty list back, because the loop body never runs. The lesson guarantees at least one score, but the code handles the empty case anyway.
Edge cases and common mistakes
- Wrong order of checks. If the first test were
score >= 60, a 95 would get a D. The first test that is true wins, and 95 is at least 60. The highest range must go first when each test has only a lower bound. - Boundary scores. 90, 80, 70 and 60 belong to the higher grade, so the tests use
>=, not>. With>a 90 would become a B. - Returning inside the loop. Only one letter would come back, not one per score.
- Order. The result has one letter per score in the input's order, including repeated scores.
Testing
[95, 82, 71]gives['A', 'B', 'C'].[65, 40]gives['D', 'F'].[100, 90, 89]gives['A', 'A', 'B'].[0, 59, 60, 69, 70, 79, 80, 89]gives['F', 'F', 'D', 'D', 'C', 'C', 'B', 'B']: every boundary lands in the expected range.
Python prints strings in a list with single quotes; the values are the same strings as "A" and "B".
