Skip to content

Checkpoint: Minutes by Topic

The Course
•
25 min study

Today you've seen strings, lists and dictionaries one at a time. This checkpoint uses all of them on one small problem. Think about which structure suits each job: a list keeps entries in their original order, and a dictionary answers "how much for this topic so far?".

Warm up: split one entry

Each entry in this checkpoint is text in the form topic:minutes. int.Parse converts text to a number; you'll see safer ways to do this on Day 6. Predict what this prints.

One line of text, two values C#
string entry = "Loops:30";
string[] parts = entry.Split(':');

string topic = parts[0];
int minutes = int.Parse(parts[1]);

Console.WriteLine(topic);
Console.WriteLine(minutes + 15);
Check your prediction
Text
Loops
45

Without int.Parse, parts[1] + 15 would join text to give 3015. Try it. The type tells you what + is going to do.

Build it yourself

Exercise

Minutes by topic

Write TotalsByTopic(List<string> entries), which returns a SortedDictionary<string, int> of minutes per topic. Use it on the entries Loops:30, Lists:20, Loops:15 and Records:10, and print this report:

  • Lists: 20
  • Loops: 45
  • Records: 10
  • Total: 75 minutes across 3 topics

Also check that an empty list of entries gives an empty dictionary, using the Check helper from Day 3.

Your workspace C#
var entries = new List<string> { "Loops:30", "Lists:20", "Loops:15", "Records:10" };
Console.WriteLine(entries.Count);
Hint 1: the idea

Split each entry, parse the minutes, and add them to whatever total the topic already has.

Hint 2: a first step

Write a foreach over the entries that just prints each topic and number. Add the dictionary once the splitting works.

Show one solution
C#
int failures = 0;
Check(TotalsByTopic(new List<string>()).Count == 0, "no entries gives no topics");

var entries = new List<string> { "Loops:30", "Lists:20", "Loops:15", "Records:10" };
var totals = TotalsByTopic(entries);

int overall = 0;
foreach (var (topic, minutes) in totals)
{
    Console.WriteLine($"{topic}: {minutes}");
    overall += minutes;
}
Console.WriteLine($"Total: {overall} minutes across {totals.Count} topics");

void Check(bool passed, string name)
{
    if (!passed)
    {
        Console.WriteLine($"FAILED: {name}");
        failures++;
    }
}

static SortedDictionary<string, int> TotalsByTopic(List<string> entries)
{
    var totals = new SortedDictionary<string, int>();
    foreach (string entry in entries)
    {
        string[] parts = entry.Split(':');
        totals.TryGetValue(parts[0], out int current);
        totals[parts[0]] = current + int.Parse(parts[1]);
    }
    return totals;
}
Text
Lists: 20
Loops: 45
Records: 10
Total: 75 minutes across 3 topics

A SortedDictionary keeps its keys in order, so the topics print alphabetically with no sorting step. TryGetValue gives 0 for a topic not seen yet, so the first and later entries for a topic go through the same line. The overall total is added up while printing, which keeps it consistent with the lines shown.

Add the entry Broken with no colon, and then Maths:ten. Each one crashes the program in a different way, and neither is handled. Read the two messages. Day 6 teaches you how to survive bad input like this.

Was this article helpful?

Your response is saved on this device.