Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
When we need to quickly look up items in a collection, a dictionary is the way to go.
System.Collections.Generic.Dictionary
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', "-----"},
{',', "--..--"},
{'.', ".-.-.-"},
{'?', "..--.."},
{';', "-.-.-."},
{':', "---..."},
{'/', "-..-."},
{'-', "-....-"},
{'\'', ".----."},
{'"', ".-..-."},
{'(', "-.--."},
{')', "-.--.-"},
{'=', "-...-"},
{'+', ".-.-."},
{'@', ".--.-."},
{'!', "-.-.--"},
{'Á', ".--.-"},
{'É', "..-.."},
{'Ö', "---."},
{'Ä', ".-.-"},
{'Ñ', "--.--"},
{'Ü', "..--"}
};
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Sets are very efficient at determining
if an object is already in the set.
0:00
However in order to determine
if an item is in the set
0:05
we need to already have the item.
0:09
When using a set we aren't
technically searching for the item.
0:11
We're only checking to see if the item
we already have is in the set.
0:15
Instead of all ready
having the item in hand
0:20
it's more common to only have some piece
of information that's unique to the item.
0:23
We call this piece of information a key.
0:27
For example schools usually have a numeric
identifier for each student in the school.
0:31
This ID can be used as a key to find
the actual student object it belongs to.
0:36
There's a collection type that's
designed to map keys to items.
0:43
It's called a dictionary.
0:47
It's aptly named because dictionaries
like the ones we use for
0:50
finding the definitions of words
map words to their definitions.
0:53
If we can find the word then
we'll have it's definition.
0:58
Dictionaries are used for
all sorts of stuff.
1:02
One thing dictionaries
are particularly good at
1:05
is translating from one value to another.
1:08
Let's experience the full
power of a dictionary
1:11
while we create an app to
translate from text to Morse code.
1:14
To start, open a new workspace by
clicking on the button on this page.
1:18
Morse code has nothing
to do with programming.
1:24
It's simply a method to communicate
using a series of long and
1:26
short signals, these signals can be
written down using dots and dashes for
1:29
example the three letters SOS is a well
known message sent using Morse code
1:34
when writing it we type dot dot dot
space dash dash dash space dot dot dot.
1:39
Because three dots is the Morse code for
an s and three dashes is Morse code for 0.
1:46
If you're unfamiliar with Morse code I've
included a link to a Wikipedia article in
1:53
the teacher's notes.
1:57
The first thing we want to do is to create
a look up table that given a single
1:58
character of text gives us
the Morse code for that character.
2:02
This is a perfect use case for
a dictionary.
2:06
Unlike lists and sets the only take
a single generic type parameter
2:09
dictionaries take two type parameters,
the first is the type of the key.
2:13
The second is the type of
the value that the key maps to.
2:18
The key and
the value can be of any type of object.
2:21
All of the keys of a dictionary
must be unique though
2:24
in this way the keys of a dictionary
behave very much like the items in a set.
2:27
Keys of a dictionary must also have
appropriate hash codes in order for
2:32
the key to work.
2:35
Just like with hash set dictionary uses
the hash codes to quickly find the keys.
2:37
Dictionaries are sometimes referred
to as hash tables because they map
2:42
hashed key values to
items in the dictionary.
2:46
Inside the Morse Code translator
class let's create a dictionary
2:50
to store the table of
characters to Morse codes.
2:53
So say private static and notice that
Morse code translator is a static class.
2:56
So all the members in the class
also need to be static.
3:03
And here we'll declare the dictionary and
3:06
the type of the key will be a char and
the type of the value will be a string.
3:11
So we're mapping character to strings.
3:17
Will give it a name say textToMorse
= new Dictionary right now.
3:21
There we go.
3:35
We can initialize a dictionary just like
we did with the hash set list in and
3:36
array only we need to
provide both keys and values.
3:41
So type our opening curly brace here and
3:45
the way we provide both a key and a value
is inside of another set of curly braces.
3:49
So I'll say A Maps to dot dash.
3:55
So now this is a dictionary
with one value in it.
4:01
And it's a mapping from the character
a to it's Morse code equivalent dot.dash.
4:04
In the teacher's notes you'll
find all 57 key value mappings.
4:10
You can copy them into the code here.
4:14
Now that we have our dictionary
constructed let's create a method named
4:22
toMorse that when passed
a string of text will
4:27
translate the text into the Morse code and
then return it.
4:30
So down here I'll say public
static string ToMorse and
4:34
it will take another string.
4:40
We'll just call it input.
4:45
In here we'll create a list to store
all the translated characters of text.
4:49
This list will have as many
items as there are characters
4:53
in the string that's passed in.
4:56
So we can just go ahead and
give the list an initial capacity.
4:58
So say list of strings output.
5:02
So this will be a list of all the Morse
code codes for the equal to a new List
5:07
of string and we'll give it an initial
capacity of the length of the input.
5:14
So we'll say input.length.
5:19
This will make it so that the list doesn't
need to be resized as we add items to it.
5:20
Now all we need to do is loop through
the characters of the string and
5:27
look them up in the dictionary one by one.
5:30
So write a for each loop.
5:32
We'll say foreach char
5:33
character because strings
are composed of characters
5:37
in input.we should first convert the input
5:42
string here to uppercase since we only
have uppercase letters in the dictionary.
5:48
So say ToUpper to find
an item in a dictionary
5:53
we simply index into it just like
we did with the arrays on list.
5:57
Only instead of using a number for
6:02
the index we'll use the key which
in this case is a character.
6:03
So I'll say string
morseCode = textToMorse.
6:08
And we'll pass in the character here.
6:19
So now this is looking up
the character in the dictionary
6:23
using this index operator here.
6:28
And then when it finds
the value it assigns it
6:31
to the Morse code variable here.
6:35
Now we just add the Morse code
that it found to our output list.
6:37
So we'll say .AddmorseCode.
6:40
Finally we need to create a string out of
our list of translated Morse code strings.
6:47
For this we'll use string
static join method.
6:52
So say return string.Join.
6:56
The join method excerpts any
innumerable collection of strings and
7:00
concatenate all the items together.
7:05
It joins them together with whatever
we pass in as the first parameter here.
7:08
We'll join them together with spaces and
7:12
here's we'll pass in our
list of output strings.
7:15
The string.Join method is
very handy when we want to
7:18
add something like a comma
between the items of a list.
7:21
In this case we want to use
a single space because Morse code
7:24
are printed with the space
between each character.
7:27
There is one more thing
we need to consider.
7:30
We've only provided the code for
57 characters in the dictionary.
7:32
What if the user enters in a character
that isn't in the dictionary.
7:36
If we attempt to retrieve a value for
7:41
a key that isn't in the dictionary then
a key not found exception will be thrown.
7:42
We should put a try catch around where
we're indexing into the dictionary here
7:48
to try to catch this exception.
7:52
So say try.
7:57
Then down here catch and
7:58
we'll catch the KeyNotFoundException.
8:02
Now we need to decide what to do
when the character can't be found.
8:11
How about we just add
an exclamation mark to the output.
8:17
Morse code only consists of dots and
dashes.
8:20
So when the user sees an exclamation
mark they'll know that
8:23
it represents a character that couldn't
be translated into Morse code.
8:26
We'll say output.Add and
an exclamation mark.
8:29
All right we've got our Morse Code
translator class written.
8:35
Now we just need to provide a way for
users to enter the text
8:39
they want to convert before we do that
let's make sure that this compiles.
8:42
Looks good.
8:53
Back in main let's first print a colon
to prompt the user to enter some input.
8:55
We need to add a user statement up here.
9:02
Now we can say Console.Write and
we just print a colon.
9:05
Now we'll read what the user types in.
9:12
And store it in a variable called input.
9:14
So say string input = Console.ReadLine.
9:17
Now we need to call out two Morse
method to translate the input into
9:24
the Morse code.
9:29
So say string output =
9:32
MorseCodeTranslator.ToMorse no
pass on the input here.
9:35
Now we just need to print out that output.
9:46
So say Console.WriteLine output.
9:48
Let's keep prompting until
the user wants to quit.
9:54
So let's wrap this all in a while loop.
9:57
So say while true indent all this and
let's just make it so
10:00
that if they want to exit the program
they just don't type anything at all.
10:09
So here under the read line.
10:13
We'll type if and
10:15
then we'll use a static method on the
string class called is null or whitespace.
10:16
So say string.IsNullOrWhiteSpace and
10:21
all this does is just check to
see if the contents of the input
10:26
variable our null or if they're just
whitespace like new lines or spaces.
10:31
And if they don't enter anything at
all we'll just break out of this loop.
10:39
Now let's compile this and
run it to see how it works.
10:42
Compiles first let's see
what the executable is.
10:50
Okay so
it's called MorseCodeTranslator.exe.
10:55
So mono MorseCodeTranslator.exe.
10:58
Okay let's type in a string here
that we want to see translated.
11:03
So I'll type in Hello World.
11:06
And there's the Morse code for
our message.
11:11
This slash here is placed in between
words wherever there's a space.
11:13
Dictionaries behave just
like sets internally.
11:18
So looking up items or
calling contains is just as fast as a set
11:21
this makes dictionaries a great
collection for doing fast look ups.
11:25
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up