Exercise: Collections

Solve 14 problems with List<T> and Dictionary<TKey, TValue> (filtering, searching, counting, looking up and combining) and check each one with tests.

Apprentice · C# · Updated

Lists and dictionaries are the collections you'll use most in C#. In this exercise you'll use each of them the way real programs do: building new lists from old ones, finding things, counting things, and looking values up by name.

Get the exercise code

Open this lesson's GitHub repository, github.com/ask-codey/exercise-collections, also linked from the Exercise box beside the lesson. Click Code, then Download ZIP, and unzip it into your bootcamp folder. Or clone it with Git.

File Methods
ListProblems.cs ArrayToList, ListToArray, LongWords, Largest, OddOnly, AppearsTwice, FizzBuzz
DictionaryProblems.cs Mascot, WordCount, CartTotal, CombineStock, LetterGrades, MostWins, FirstLetters

Open CollectionsExercise.slnx in Visual Studio, replace each method's return line with your own code, and run the tests with Test > Run All Tests.

Reading the examples

The comments show lists in square brackets and dictionaries in curly braces, with each key and its value:

LongWords(["a", "train", "is", "late"], 4) -> ["train", "late"]
WordCount(["pizza", "tacos", "pizza"]) -> { "pizza": 2, "tacos": 1 }

Hints

  • A new list: start with new List<string>(), loop through the input, and Add what you keep. Don't change the list you were given.
  • Largest: start with the first number, not 0: the numbers might all be negative.
  • A lookup table: build the dictionary inside the method, then look the key up. To ignore capital letters, store lowercase keys and look up school.ToLower().
  • Counting: use the thing as the key. If the key's already there, add 1; if not, set it to 1.
  • Combining: new Dictionary<string, int>(firstStore) makes a copy you can change safely.
  • The keys of FirstLetters are chars: word[0] is a word's first letter, and char.ToLower(letter) lowercases one.

When you're done

Every test is green. Look at the methods that build a list or dictionary: do they all leave their inputs unchanged? A method that quietly changes what it was given is a common source of bugs.

Submit your exercise

The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here.

  1. On GitHub, click +, then New repository. Name it (for example exercise-collections), choose Public, and click Create repository. Leave out the README: your folder already has one.

  2. In a terminal in your exercise folder (Git Bash on Windows), send your work to your new repo. If you downloaded the ZIP:

    git init -b main
    git add .
    git commit -m "My solution"
    git remote add origin https://github.com/your-name/exercise-collections.git
    git push -u origin main
    

    If you cloned the starter instead, point your copy at your own repo, then commit and push:

    git remote set-url origin https://github.com/your-name/exercise-collections.git
    git add .
    git commit -m "My solution"
    git push -u origin main
    
  3. Paste your repo's address, such as https://github.com/your-name/exercise-collections, into Submit your exercise on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.

Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.

So what

Filter, find, count, look up, combine: with these patterns and two collections, you can handle most of the data a program will ever give you.

Key terms

Related