Lesson 10 of 10

Dictionaries and LINQ

Goal

Counting with Dictionary and querying with LINQ

01

Explanation

A Dictionary<string, int> maps each key to a value. After var gold = new Dictionary<string, int>();, the line gold["Layan"] = 50; adds the key if it isn't there yet, or replaces its value if it is. Reading a key that doesn't exist, however, throws a KeyNotFoundException, which is why the program stops at gold["Sara"]++ the first time. So check first with ContainsKey, or use TryGetValue(key, out int value), which returns whether the key was found and puts its value in value, or zero if it wasn't. It's the same Try pattern you saw in the lesson on methods.

LINQ, from System.Linq, gives you ready-made methods for asking questions of any collection, and most of them take a short function written with an arrow, called a lambda. Where(x => x > 10) keeps the items that meet the condition, Select(x => x * 2) transforms each item, OrderBy sorts in ascending order and OrderByDescending in descending order, Sum() adds everything up, and Count(x => ...) counts the items that meet the condition. You can chain them like this: scores.Where(s => s >= 50).OrderBy(s => s).ToList().

A dictionary is a collection of pairs, each with a Key and a Value, so LINQ works on it just as it does on a list, and Values gives you just its values. Keep in mind that a query doesn't run when you write it but when you go through its result, so if the collection changes in between, the result changes too. ToList() runs it right away and stores the result in a list.

02

Your task

Complete CountKills(kills) to return a dictionary with the number of times each kind of enemy was killed in the log, KillsOf(kills, name) to count the kills of a single kind with Count, and TotalKills(counts) to add up the dictionary's values with Sum. Then complete Report(counts, min) to return a list of strings in the form "Goblin: 3" for every kind killed min times or more, sorted from most kills to fewest, using Where, OrderByDescending and Select.

Checks

5 checks
  1. CountKills counts each kind
  2. KillsOf counts one kind and returns zero for a kind that wasn't killed
  3. TotalKills adds up the dictionary's values
  4. Report keeps the kinds that reached min and sorts them from most kills to fewest, in the form "Goblin: 5"
  5. CountKills and Report work together on a full log
03

Hints

0/3
Track lessons0 of 10 done

C# is the language behind Unity games and .NET apps. Every variable has a fixed type, programs are built from classes, and it comes with ready-made tools for most of what you need.

Beginner level, 10 lessons, 2.5 hours in total.

Program.cs
OutputC# (Mono 6.6, Judge0)

Compiling and running your code on the server.

–/5

Compiling and running your code on the server.

5 checks listed under “Your task”.

Track lessons0 of 10 done

C# is the language behind Unity games and .NET apps. Every variable has a fixed type, programs are built from classes, and it comes with ready-made tools for most of what you need.

Beginner level, 10 lessons, 2.5 hours in total.