Problem overview
Given an int[] of scores from 0 to 100, letterGrades returns a list with one letter per 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 methods. The helper letterGrade(int score) turns one score into its letter. The method letterGrades(int[] scores) goes over all the scores and collects the helper's answers.
For scores = [95, 82, 71] the result is ["A", "B", "C"]. For [65, 40] it is ["D", "F"]. For [100, 90, 89] it is ["A", "A", "B"]: 90 is the lowest A, and 89 is one point short of it.
Approach
There are two separate questions here. "Which letter does this score get?" is a decision about one number. "Do that for every score and keep the answers" is a loop. Writing each as its own method keeps both short, and the loop never has to know anything about grade ranges.
For the helper, the trick is to check the ranges from the top down. A score of 90 or more is an A. If the code gets past that check, the score is below 90, so "at least 80" already means 80 to 89, and no upper bound has to be written. Each if only needs one comparison, because every earlier one has filtered out the higher scores.
Take the score 82. The test 82 >= 90 is false, so the method moves on. The test 82 >= 80 is true, so it returns "B" and the remaining checks never run. A score of 40 fails all four tests and falls through to "F".
The cost is small: each score takes at most four comparisons, and the loop visits each score once, so the work grows in step with the number of scores.
Step by step
The helper, letterGrade
- If
score >= 90, return"A"at once. Areturnends the method, so the checks below only see scores under 90. - If
score >= 80, return"B". Scores of 90 and up have already left, so this means 80 to 89. - If
score >= 70, return"C". - If
score >= 60, return"D". - After the last
if, return"F". Only scores below 60 get this far, so the line needs no test and noelse.
The collector, letterGrades
- Create an empty
List<String>namedgrades, backed by a newArrayList. The letters will be added one at a time, so it starts empty. - Write a for-each loop with an
intnamedscoreoverscores. It runs once per score, so each score produces exactly one letter. - Inside the loop, call
grades.add(letterGrade(score)). The inner call runs first and returns aString, whichaddthen appends. Appending keeps the letters in the order of the scores. - After the loop, return
grades. Thereturngoes after the loop's closing brace: inside the loop, only the first letter would ever come back.
The solution in Java
class Solution {
public String letterGrade(int score) {
if (score >= 90) {
return "A";
}
if (score >= 80) {
return "B";
}
if (score >= 70) {
return "C";
}
if (score >= 60) {
return "D";
}
return "F";
}
public List<String> letterGrades(int[] scores) {
List<String> grades = new ArrayList<>();
for (int score : scores) {
grades.add(letterGrade(score));
}
return grades;
}
}
Java details in this code:
- Two methods, one class.
letterGradescallsletterGradeby its name alone, because both belong toSolution. The helper's parameter is also calledscore, but each method has its own variables: the call hands the helper a copy of the loop's current score. - Array in, list out. The input is a fixed-size
int[], but the result is aList<String>because the lesson builds it withadd. An array's size is fixed when it is created, while anArrayListgrows as you add. - The declared type is the interface.
List<String> grades = new ArrayList<>();declares the variable as aListand creates anArrayList. The<>tells Java to fill in theStringtype from the left side. - The imports.
ListandArrayListlive injava.util, so a standalone program needsimport java.util.*;. - Printing a list. A
Listprints in square brackets without quotes around the strings, for example[A, B, C]. The values are still strings.
Edge cases and common mistakes
- Boundary scores. The values 59, 60, 69, 70, 79, 80, 89 and 90 are where off-by-one mistakes show up. Using
> 90instead of>= 90would turn a 90 into a B. With the list[0, 59, 60, 69, 70, 79, 80, 89], the method returns[F, F, D, D, C, C, B, B]. - Checking from the bottom up. If the first check were
score >= 60, a 95 would return"D"immediately. Without upper bounds, the order of the checks matters: the highest range goes first. - Returning inside the loop. A
returnthere endsletterGradesafter the first score. - Comparing strings. If you later compare grades, use
equals, as ingrade.equals("A"). The==operator on strings compares references, not text.
Testing
These calls return the following lists:
letterGrades(new int[]{95, 82, 71})returns[A, B, C].letterGrades(new int[]{65, 40})returns[D, F].letterGrades(new int[]{100, 90, 89})returns[A, A, B].
