Dictionaries

When we need to quickly look up items in a collection, a dictionary is the way to go.


When using a set we aren't technically searching for the item. We're only checking to see if the item we already have is in the set.

It's more common to only have some piece of information that's unique to the item. We call this pieace of information a key, like (ID number, registration number, etc). So we could search for the key to find our desired information.

There's a collection type that's designed to map keys to items, called dictionary.

Let's create our program, 'MorseCode Translator'. First, initialize the dictionary. Dictionary takes 2 parameters (key, value).

// in MorseCodeTranslator class
private static Dictionary<char, string> _textToMorse = new Dictionary<char,string>()
{
  {' ', "/"},
  {'A', ".-"},
  {'B', "-..."},
  {'C', "-.-."},
  {'D', "-.."},
  {'E', "."},
  {'F', "..-."},
  {'G', "--."},
  {'H', "...."},
  {'I', ".."},
  {'J', ".---"},
  {'K', "-.-"},
  {'L', ".-.."},
  {'M', "--"},
  {'N', "-."},
  {'O', "---"},
  {'P', ".--."},
  {'Q', "--.-"},
  {'R', ".-."},
  {'S', "..."},
  {'T', "-"},
  {'U', "..-"},
  {'V', "...-"},
  {'W', ".--"},
  {'X', "-..-"},
  {'Y', "-.--"},
  {'Z', "--.."},
  {'1', ".----"},
  {'2', "..---"},
  {'3', "...--"},
  {'4', "....-"},
  {'5', "....."},
  {'6', "-...."},
  {'7', "--..."},
  {'8', "---.."},
  {'9', "----."},
  {'0', "-----"},
};

Then, we create method ToMorse that takes input as string and return the morse code.

// in MorseCodeTranslator class
public static string ToMorse(string input)
{
  // 1
  List<string> output = new List<string>(input.Length);

  // 2
  foreach(char character in input.ToUpper())
  {
    // 3
    try
    { 
      // 4
      string morseCode = _textToMorse[character];
      // 5
      output.Add(morseCode);
    }
    catch(KeyNotFoundException)
    {
      output.Add("!");
    }
  }
  // 6
  return string.Join(" ", output);
}
// in Main class

// 7
string input = Console.ReadLine();
// 8
string output = MorseCodeTranslator.ToMorse(input);
// 9
Console.WriteLine(output);

The explanation of code above:

  1. Initialize a list named output to store the morse code.
  2. Convert the input from user (string) to character (char) and loop for each char.
  3. Using try catch if the character doesn't exist in our dictionary.
  4. Check the key from _textToMorse dictionary, returns the value, and store it in morseCode (string).
  5. Inserting the string morseCode into list named output.
  6. Concatenate our list to be a string and return it back to main function.
  7. Asking for user's input.
  8. Throw the input to ToMorse method in MorseCodeTranslator class.
  9. Display the translated morse code output.

Other Collection Types

  • LinkedList: Linked list is different than the list collection type we used earlier in this course. It used when we want to add or remove items without the items in the list being shifted. It works because linked lists is a individual nodes that are linked to each other, instead of using an array internally. It use queue and stack, you'll learn it in Algorithm & Data structure week.
  • SortedSet
  • SortedDictionary
  • SortedList

results matching ""

    No results matching ""