Problem overview
The function letterGrades receives a vector of integer scores and returns a vector<string> 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 scores [95, 82, 71] give [A, B, C]. The scores [65, 40] give [D, F]. The scores [100, 90, 89] give [A, A, B]: 90 is the lowest A, and 89 is one point short.
Approach: one function decides, the other repeats
The lesson splits the task in two. The helper letterGrade answers a single question: which letter does this one score get? The function that is checked, letterGrades, loops over the vector and asks the helper once per score. Each function does one job, and the helper can be tested on its own.
Inside the helper, the order of the checks does the work. Test the highest range first. A return ends the function, so by the time the check for 80 runs, every score of 90 or more has already left. At that point "at least 80" means "80 to 89", and the upper bound never needs to be written. The same holds for the checks that follow, and the final "F" needs no test: only scores below 60 reach it.
Take the score 89 from the third example. It fails score >= 90, so the function moves on. It passes score >= 80, so the function returns "B" and stops; the checks for 70 and 60 never run. A score of 40 fails all four tests and reaches the final return "F".
Step by step
- Return
"A"whenscore >= 90. Start from the top range. - Return
"B"whenscore >= 80. Only scores below 90 get here. - Return
"C"whenscore >= 70. - Return
"D"whenscore >= 60. - Return
"F"after the lastif. Everything below 60 ends up here. - In
letterGrades, declare an emptyvector<string>namedgrades. The letters will be added one at a time. - Loop over
scoreswith a range-basedforand anintnamedscore. The loop runs once per score. - Append the helper's answer.
grades.push_back(letterGrade(score))adds the letter at the end, which keeps the original order. Both functions are in the same class, so the call needs only the function's name. - Return
gradesafter the loop. Returning inside the loop would send back only the first letter, or nothing at all.
The solution in C++
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";
}
vector<string> letterGrades(vector<int> &scores) {
vector<string> grades;
for (int score : scores) {
grades.push_back(letterGrade(score));
}
return grades;
}
};
C++ details:
- String literals become
string.return "A";is a C-style string literal, which is converted to astd::stringbecause the function's return type isstring. - The same name in two places. The helper's parameter and the loop variable are both called
score. They are separate variables: the callletterGrade(score)copies the loop's value into the helper's parameter. push_backgrows the vector. Each call adds one element at the end, so after the loopgradeshas exactly as many letters asscoreshas numbers.- The vector is taken by reference. The
&avoids copying the scores.
Testing the helper on its own
Because letterGrade is an ordinary member function of Solution, it can be called directly with a single score: letterGrade(79) returns "C" and letterGrade(80) returns "B". Checking the boundaries of the helper before the loop exists is the main practical gain of the split: when the output of letterGrades is wrong, you know whether the fault is in the grading rule or in the loop that collects the results.
Why not one long condition per range?
You could write each test with both ends, such as score >= 80 && score <= 89. It works, but every boundary then appears twice, once as the top of one range and once as the bottom of the next, and a typo in either place opens a gap or an overlap. Because each return removes the scores it handled, the version in the lesson states each boundary exactly once.
Another option is a chain of else if. It behaves the same here, since every branch returns, and it is a matter of taste. The lesson's form, with plain if statements that return, keeps every test at the same indentation and shows clearly that return ends the function.
Edge cases and common mistakes
- Boundaries. The scores 0, 59, 60, 69, 70, 79, 80, 89, 90 and 100 give
F, F, D, D, C, C, B, B, A, A. Writing>instead of>=would shift every boundary score down a grade. - Checking the lowest range first. If the first test were
score >= 60, a score of 95 would return"D". The tests must go from high to low. - Returning inside the loop. Only one letter would be produced.
- A missing final return. A function that can end without returning a value has undefined behaviour in C++. The closing
return "F"covers every score that passed no earlier test.
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, 90, 100]gives[F, F, D, D, C, C, B, B, A, A].
