Start learning free

LESSON 242 OF 289 · HELPER FUNCTIONS

Letter Grades in C#: A Helper Method and a List

Practice Read the full explanation

Given an array of integers scores, return the letter grade of every score, in the same order as the scores.

Each score gets one letter:

  • 90 to 100: "A"
  • 80 to 89: "B"
  • 70 to 79: "C"
  • 60 to 69: "D"
  • 0 to 59: "F"

The work is split between two functions. The first one is a helper: it gets one score and returns its letter. The second one, the function that is graded, goes over the scores and collects the helper's letter for each of them.

 

Example 1:

Input: scores = [95, 82, 71]
Output: ["A", "B", "C"]
Explanation: 95 is in the A range (90 to 100), 82 is in the B range and 71 is in the C range.

Example 2:

Input: scores = [65, 40]
Output: ["D", "F"]
Explanation: 65 is between 60 and 69, so it gets a D. 40 is below 60, so it gets an F.

Example 3:

Input: scores = [100, 90, 89]
Output: ["A", "A", "B"]
Explanation: 100 and 90 are both in the A range: 90 is its lowest score. 89 is one point short of it, so it gets a B.

 

Constraints:

  • scores has at least one score.
  • Every score is an integer from 0 to 100.
  • The result has one letter per score, in the same order as scores.
STEP 1 OF 9
    Ctrl + Enter to check

    C# · HELPER FUNCTIONS

    Letter Grades in C#, explained

    Problem overview

    Given an int[] of scores from 0 to 100, LetterGrades returns a List<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 work is split in two. LetterGrade is a helper that turns one score into its letter. LetterGrades goes over the array and collects the helper's answers. 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: check the highest range first

    The helper is a chain of if statements, and the order is what makes it short. Test the top range first: if the score is at least 90, return "A". Every test below that only runs for scores under 90, so it needs no upper bound. "At least 80" really means "80 to 89" at that point, because the scores of 90 and up have already left the method through their return.

    The same idea works the other way round only with more writing: you would need to check score >= 80 && score < 90 and so on. The top-down order replaces each of those with one comparison.

    Step by step

    1. In LetterGrade, return "A" when score >= 90. return ends the method immediately.
    2. Return "B" when score >= 80, then "C" when score >= 70 and "D" when score >= 60. Each test sees only the scores the earlier ones let through.
    3. Return "F" after the last if. Only scores below 60 get there, so this line needs no test and no else.
    4. In LetterGrades, create an empty List<string> named grades. The letters will be added one at a time.
    5. Loop with foreach (int score in scores). The loop runs once per score, so the list gets exactly one letter per score.
    6. Inside the loop, grades.Add(LetterGrade(score)). One method calls another in the same class by its name. The helper has its own parameter, also called score, but each method has its own variables: the call hands it the loop's current score.
    7. Return grades after the loop. A return inside the loop would stop after the first letter.

    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";
        }
    
        public List<string> LetterGrades(int[] scores) {
            List<string> grades = new List<string>();
            foreach (int score in scores) {
                grades.Add(LetterGrade(score));
            }
            return grades;
        }
    }
    

    C# details:

    • The method has the return type string, and the compiler checks that every path returns one. The final return "F"; is what covers the path where no if matched.
    • A List<T> grows as you Add to it, unlike an array, whose length is fixed when you create it. That suits a result whose size is only known by looping.
    • List<T> lives in System.Collections.Generic. In a file of your own, add using System.Collections.Generic;.
    • Strings are compared with == by content in C#, so tests can check the returned letters directly.

    Following one call through

    Take LetterGrades(new int[] { 95, 82, 71 }). The list starts empty. The first pass of the loop calls LetterGrade(95): 95 is at least 90, so the helper returns "A" and the list holds [A]. The second pass calls LetterGrade(82). The first test fails because 82 is below 90, the second passes, and "B" is added. The third pass sends 71 past the tests for 90 and 80 and stops at "C". After the loop the list is [A, B, C], and that list is what the method returns.

    Notice that the loop never looks at ranges. It doesn't know what a "B" is, and it doesn't have to. If the school's cutoffs changed, only LetterGrade would change, and LetterGrades would stay as it is. That separation is the reason to write a helper even for a rule this small.

    Why the order of the tests matters

    Reverse the chain and the code breaks quietly. If the test for 60 came first, then 95 would meet score >= 60 before anything else and come back as "D". The code would still compile and run; it would just give wrong letters. Whenever ranges are tested with a single comparison each, they must go from the most demanding to the least, so that the first test that passes is the right one.

    Edge cases and common mistakes

    • The boundaries. 90, 80, 70 and 60 each belong to the higher grade, which is why the tests use >=. Using > would turn 90 into a "B". The scores 0, 59, 60, 69, 70, 79, 80 and 89 give [F, F, D, D, C, C, B, B].
    • Checking the lowest range first. With score >= 60 tested first, every passing score would get "D".
    • Returning inside the loop. Only the first letter would come back.
    • A score of exactly 100. It is at least 90, so it gets "A" like the rest of its range.

    Testing

    scoresResult
    [95, 82, 71][A, B, C]
    [65, 40][D, F]
    [100, 90, 89][A, A, B]
    Keep going with C#1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free