typestar

grade_report.cs in C#

A gradebook in one file: averages, letter grades and a summary table.

// grade_report: per-student averages, letter grades, a class summary
var scores = new Dictionary<string, int[]>
{
    ["Ada"] = new[] { 92, 88, 95 },
    ["Alan"] = new[] { 78, 85, 80 },
    ["Grace"] = new[] { 90, 91, 86 },
    ["Edsger"] = new[] { 70, 65, 74 },
};

string Letter(double avg) => avg switch
{
    >= 90 => "A", >= 80 => "B", >= 70 => "C", _ => "F",
};

var averages = scores
    .Select(pair => (Name: pair.Key, Avg: pair.Value.Average()))
    .OrderByDescending(s => s.Avg)
    .ToList();

Console.WriteLine("student    avg  grade");
foreach (var (name, avg) in averages)
{
    Console.WriteLine($"{name,-9} {avg,5:F1}  {Letter(avg)}");
}

var classAvg = averages.Average(s => s.Avg);
Console.WriteLine($"\nclass average {classAvg:F1}");
Console.WriteLine($"top student   {averages[0].Name}");

How it works

  1. A dictionary of score arrays is the whole data model.
  2. The Letter local function maps averages through a switch expression.
  3. LINQ orders the students; alignment holes format the table.

Keywords and builtins used here

The run, in numbers

Lines
28
Characters to type
776
Tokens
195
Three-star pace
95 tpm

At the three-star pace of 95 tokens a minute, this run takes about 123 seconds.

Type this snippet

Step 2 of 2 in Encore, step 29 of 29 in Language basics.

← Previous