Hello, welcome to the third challenge for the section 20, part of the course.
I’m in the section 20 workspace, and I've got two projects,
the challenge_3 project and the challenge_3_solution project.
So, as usual the solution is my solution, and the challenge_3
project is the shell that I'm giving you, that has the beginnings of
the project that you can work on.
So, let's talk about this project.
This is a fun challenge.
This challenge has two parts, and it's all about understanding
stood set and stood map.
We're going to use the sets and the maps together.
We're going to be reading words from a text file provided,
I'm providing that for you.
The text file is called words.txt and I'll show that to you in just a moment.
And it contains the first few paragraphs from the book,
The Wonderful Wizard of Oz.
So, the idea here is, the first part of the challenge is you're going to
read each word from that file and count how many times it occurs in the file.
And then display that information.
So, what we get is a listing similar to this.
This is just the first few words.
You can see that the word Aunt appears five times in the text,
Dorothy eight, Dorothy's once, Em 5, Even once, and from once.
Now you'll also notice that these words are displayed just
like this A, D, E, F, they're displayed in ascending order.
And then right next to the word, we have the number
of times that it occurs.
Simple!
So, please use a map for this, and make sure that your key value pair
is a string.int, that makes sense.
This is your key right here.
The word and the value is the count.
And the functions that I've written for you assume this, so
make sure you use the string.Int.
So, that's part one.
And let me show you the file real quick before we talk about part two.
Here's the file, you can see it right here in words.txt.
There's the file and it's the first, I think five, or six, or seven paragraphs
from The Wonderful Wizard of Oz.
So, you can see Dorothy appears here, and it's really easy to
check your work in code light.
For example, if I just double click Dorothy, you can see the Dorothy
appears once, twice, three times, four, five, six, seven, eight, nine times.
So, it's really easy to tell just because it highlights it in yellow, and
it also gives you the line number that it appears on, which is really cool.
Because that's what part two of the challenge is all about.
Alright, so let me get back to the description.
So, that's part one, and then in part two what I want is, same
idea, I want to read all the words from the file and I don't really care how many
times they occur. I've already got that up here.
What I want to know is what line numbers do they occur on.
I want to see all the line numbers.
So, in this case, we can do this one, Aunt appears on line
number 2, 7, 25, 29 and 48.
Now, it might appear more than once on each line, I don't want
you to say 2-2, 7-7-7 and so forth.
Just the line number should appear only once.
So, again the word should be ascending in order and the line number should
be ascending in order in here as well.
So, one more time, Aunt appears on line 2, 7, 25, 29 and 48.
So, we can come over here to words.txt, we can find Aunt which is right there.
So, there it is 2, 7, 25, 29 and 48.
It's a nice easy way to test your output.
That's the challenge.
So, let me go back to the description and we'll talk a
little bit more about this.
For this part, for part two, please use a map of string, set,
Int, key value pairs, right.
So, the key is the word, the string, and the value is a set of integers.
We did this in the description for the map video.
This is just going to apply it now.
For this one as well, for part two, consider using a string
stream to process your words.
So, in other words, read the line from the file into a string.
And then use a string stream to process that string, so you can extract the
words from it really, really, easily.
I've also provided the basic shell for both of these.
And I'll show you that in a second.
And I've provided a function called clean string.
And you can pass a word into that function, and it's going to
return the clean version of it.
So, it's going to remove any trailing periods, or semicolons,
or colons, and things like that.
And you could tweak that function however you like.
I'll show you the code in a second.
That's it. Let me run this program, so you can see the output.
And you can see all the display I'm going to scroll all the way up
to the top, it's running part one and then it's running part two.
Here's part one, Aunt appears five times, Dorothy appears eight times,
Em appears five times, and you go all the way through Aunt and lowercase
Aunt would be different words.
You can choose to make them all upper, or lower, or do whatever you
like with that just to have fun.
So, in this case, let's look at some of the common words in
English, the ‘a’ appears 20 times, ‘and’ appears 32 times, ‘the’ is
a very popular word, and there is ‘the’, it appears 43 times.
So, that's part one.
Again notice that this is sorted in ascending order.
Now here's part two, we have the word and the occurrences
of that word with line numbers.
So, Aunt appears on lines 2, 7, 25, 29 and 48, here you can see
that Henry appears on lines 2, 6, 35, 45, and 50, ‘There’ with
the capital T appears on line 2.
And you can see the rest.
They're all here.
So, that's the challenge.
It's not that hard.
Don't overthink this challenge.
Let the STL do the work for you.
Don't start messing around, and trying to get into the details of this,
just let the STL do the work for you.
Think abstractly.
So, that's a sample run, and let me show you the code that I'm giving you.
It's right in here in challenge_3 project, main, CPP.
So, I'll double click there, and let me scroll down to the main real quick.
There's your main, right here.
It's running part one and then part two.
And for each part, I'm, here's part two for example, there's the file.
I'm opening the file. I'm closing the file.
So, think of part one and part two as unique little functions
that each one opens the file, each one closes the file.
So, you can see in part one right here, my map is called words.
And it's a map of string, Int, key value pairs.
I've got some variables here.
I've got my file which I'm opening.
I'm checking to see if it was open successfully.
And if it is then you write this code, and then when you're
done with your code, you call that function right there.
Display words, pass it in the map and I've written the code
that's going to display that all in a nice little format.
But this is the code you need to implement here for part two, same idea.
Here's my map words.
This time, it's a map of key value pairs where the key is a string
and the value is a set of integers as we discussed.
I'm opening the file.
I'm closing the file here if it was open successfully.
You implement this code, when you're done processing the map, pass the
map into display words, and it'll display all that information for you.
Now those two functions I wrote up here, this is display
words for part one, and this is display words for part two.
The only difference is this one expects string, Int, key value pairs.
This one expects obviously a map of those.
This expects a map of string and a set of Int key value pairs.
You can totally erase these, and do them yourself.
I would actually encourage you to do that but they're
there for your benefit.
Get rid of them, and do them when you're on your own if you like.
And then here's the clean-string function.
It expects a string, and you can see the code is dead simple for each
character in the string if it's a period, or a comma, or a semicolon, or
a colon, I'm just not processing it.
Otherwise, I'm just appending the character to the new string and
returning it, so that's it I'm just filtering on those characters.
You can add some, remove some, or do whatever you like.
That's it.
That's the challenge.
Again, have fun.
This is actually a fun challenge, and it's really easy to do.
But you'll be surprised how easy this is to do.
It doesn't seem so easy, but it really is, and as I said the best advice I
can give you is don't overthink this.
Let the STL do its job that's what it's good at.
Okay, so I will see you in the next video in the solution.
So, have fun!