SPEAKER: The last function that you'll implement in helpers.pi is substrings.
And the role of this function is going to be to do this.
You'll take in, not just string inputs a and b as you had before,
but also a variable n, which will be an integer representing the substring
length that you want to compare.
What do you do with that number n?
Well you'll split each string into all possible substrings of length n
characters.
So if you have a string that's 5 characters long
and you're looking for all substrings of length 3,
you might have a bunch of different substrings
that might overlap with each other that are all of length 3, for example.
And we'll take a look at an example of that shortly.
After that you'll want to calculate a list of all of the substrings
that appear in both files as usual.
And then just like in our prior two functions,
you'll return a list that contains all of the substrings that
appear in both of the files.
Let's take a look at an actual example of this.
Let's say that n were 3 and therefore we want to find all substrings of length 3
characters and we had input "Hello".
What would it look like if we tried to take the string hello
and turn it into all possible substrings of length 3?
Well it would look something like this.
"Hel" is a substring of length 3, as is "ell" as is "llo".
These are all substrings of length 3.
And they do overlap.
But these are all the possible ways we can
represent three contiguous characters out of the string "Hello".
If on the other hand n had been 2, now we
want to look for all substrings of length 2
of which there are more of them.
We can do "He" or "el" or "ll" or "lo" and those
are all possible valid substrings of length 2.
So what are you going to have to do here.
The first thing you want to do is extract all the substrings
from each of the strings a and b.
And remember that in Python you can use syntax like this--
s and then in square brackets i colon j to get the substring of the string
s from index i in the string, up to but not including, index j in the string.
So that might be helpful for extracting substrings.
And you may want to write some sort of helper function that can take a string
and get you all of the substrings of a particular length, which may be helpful
if you think about extracting substrings from both of your original string
inputs.
As in the prior two functions, make sure that at the end of your substring
function, you return a list of all of the matches, the substrings of length
n, that are present both in string a and string b,
making sure as before to avoid duplicate substrings that
are present in that final list.
After that, you'll be done with writing substrings
and as a result, done with three of the functions
that you'll have to implement inside of helpers.pi.