00:01
We want to write a program that reads a word and prints all of its substrings sorted by length.
00:06
So, as an example, we have the word rum as our input, and the substrings for rum, sorted by length, are first the substrings of length 1, r, u, and m, then the substrings of length 2, ru, and um, and then the substrings of length 3, which is just rum.
00:30
Notice that we have three sections here, the substrings of length 1, those of length 2, and those of length 3.
00:39
In other words, we have three sections, the length of our input, where first we have three substrings of length 1, again, the length of our input, then 3 minus 1 substrings, and then 3 minus 2 substrings.
01:10
This pattern will help us write our program.
01:15
So first, we just have to start with a user input.
01:21
So let's just call this input, and we can ask the user for a word with something like provide a word.
01:45
And we know that the length of this input is going to be important, so let's calculate that, and we can call it input length.
01:57
We're going to get that by doing length of input.
02:05
And as i mentioned earlier, we're going to have three sections, or a number of sections equal to the length of our input word.
02:14
So we can start a for loop with for i in range input length.
02:28
That way, if our input word is three letters long, like the word rum, we'll go through this for loop three times, one for each of these sections.
02:44
We know that for the first time through this loop, we're going to have to print a number of substrings equal to the length of the input again, and then for the second time through, a number of substrings equal to the length of the input minus one, and then again for the length of the input minus two, and so on, if this word was any longer.
03:10
So we can again write that in a for loop.
03:13
So we've got a nested loop here using a different letter for j in range input length minus i.
03:29
That way, our first time through, it will be for our example rum, three minus zero.
03:39
Our second time through will be three minus one, and our third time through will be three minus two.
03:52
And we know that within this for loop, we're going to have to print the actual substring.
03:58
So let me write that down here, because before we print it, we're going to have to figure out what that substring actually is.
04:10
Now let's look at these substrings in our example in terms of their indices in the input word.
04:19
First, we just have our input at index zero, then our input at index one, and our input at index two.
04:34
Then for our next section, we have our input at index zero plus our input at index one, and then our input at index one plus our input at index two.
04:53
And finally, we have our input at the first three indices.
05:15
And if this word were longer, this would continue with the input at the first four indices, and then the input at the first five indices, and so on.
05:26
Notice that the first substring of each section starts with the letter r, or the first letter in our input word, which is index zero.
05:41
And the length of the substring goes from one to two, and so on, until we get to the length of our input, in this case, three.
05:55
So if we start with our substring equal to an empty string, we can create another nested for loop.
06:09
Again, using a different letter, i'll use k here.
06:12
We can build up our substring.
06:30
So now we just have to figure out what goes in these brackets and in these parentheses.
06:39
The number of times this line is called is going to be equal to the length of the substring.
06:46
So we know that first we want it to be called just once, since we have substrings of length one.
06:55
Then we want it to be called twice for substrings of length two, and three times for substrings of length three.
07:02
So if we have this equal to i plus one, then when i equals zero, we'll have substrings of length one.
07:12
When i is one, we'll have substrings of length two.
07:16
And when i is two, we'll have substrings of length three.
07:19
And now for this section here, when j is zero, we want to start with the first letter of the word.
07:30
So in this case, r.
07:32
When j is one, we want to start with the second letter.
07:38
When j is two, we want to start with the third letter.
07:42
And so on and so forth, if we have a longer word than three letters.
07:49
And when our substring is longer than just one letter, we're just going to want to move to the next letter every time.
07:56
So we can have our index be j plus k.
08:04
Now let's see how this works for our example over here.
08:07
We start with input equals rum, therefore input length equals three.
08:25
And these values here are not going to change throughout the program.
08:30
So let's start with i equals zero, since that's how this for loop will start.
08:39
Immediately, we start this next for loop, and j also starts at zero.
08:46
And now we have substring equal to an empty string.
08:57
Then we start our next for loop with k equal to zero.
09:02
And for this next sign, substring is equal to substring plus the input at index j plus k.
09:11
J plus k is equal to zero plus zero, which is just zero.
09:22
And the input at index zero is equal to the letter r.
09:34
So our substring becomes just the letter r.
09:45
And now we re -evaluate for k, but because the range goes up to i plus one, and i in this case is zero, so one is our maximum.
09:56
And remember that's not inclusive.
09:59
So our for loop here ends, and we move on to this next line, which is print substring.
10:08
And our substring is the letter r, so the program will print the letter r.
10:17
And now we move back up here and re -evaluate j.
10:23
Our input length is three, and i is zero, so our maximum for j is three.
10:34
And because j was zero, we can move j up to one...