For this question, there are a couple of different things you're going to need to do.
You're given inputs which are a string, a pattern number and generations--
the number of times you're going to apply the pattern to the string.
The first thing we're going to need to do is to take the pattern number and convert it
into some sort of pattern that we're going to be able to use to apply to our string.
The second thing we're going to need to do is create a new string by applying
the pattern to string, and we're going to do that generation times.
Finally, we're going to need to return the last new string that we create.
Let's first look at how we can create the pattern from the pattern number.
We're given a number of each of the patterns,
and one thing I noticed about these are they are actually the powers of 2.
I'm going use that in my code. You don't have to. You can use the numbers as they are there.
But I decided to use them as powers of 2.
Now, if you're given a pattern number, you need to find out whether 128 goes into that pattern number.
I'm going to do integer division by 2^7.
If that's 1, I'm going to know that X X X corresponds to X in my new string.
That's my rule.
I decided to make a dictionary.
X X X is going to be my key and X is my value.
If it's 0 then X X X, then X X X would correspond to a dot.
Once I've done that, I need to see what the remainder is.
What the rest of the number is after I've subtracted 128 from it--if it was 1 or nothing.
I can use modulo for that.
This is going to be my new pattern number.
On my new pattern number, I'm now going to divide by 2^6.
Again, if it's 1, I'm going to make the corresponding pattern a key with value X.
If it's 0, I'm going to make the corresponding value dot.
I'm going to continue running through all the values here--all the numbers here--
down to the bottom, seeing which ones correspond to an X
and which correspond to a dot.
The way I'm going to do it is to use these numbers here
and run through those backwards.
Onto the code.
I said that I was going to set up a dictionary of my patterns.
I'll initialize patterns to be the empty dictionary.
I'm going to write all the patterns so that I can refer to them.
For convenience, I'm just going to let n equal the length of the string,
because I'm going to use it several times.
I've got two things to do.
I've got to build my patterns dictionary, and I've got to make a new string
by applying pattern to string.
I'm going to do that generations times.
First, let me build the patterns dictionary.
As I said, I'm going to loop through the numbers 7 down to 0 in reverse order.
Do exactly as I said before, check whether pattern number is divisible by 2 to the power of that number.
If that's equal to 1--strictly speaking, I don't need to actually say ==1,
because 1 counts as true.
But just for readability I'm going to put it in.
If it's equal to 1, then I'm going to subtract power from the pattern number.
If it's equal to 0, then the corresponding value is dot instead of x.
That completes the dictionary, and now we go on to the loop,
which is going to run generations times to change the string into a new string.
For j in the range of 0 up to generations, I'm going to make a new string,
which is the empty string.
I'm going to loop through the old string or through the indexes in the old string.
What we're going to do now is step through the string, looking at every pattern.
So we need to build up our pattern.
What is a pattern?
It's the central character that we're looking at,
and it's the neighbor the left and the neighbor to the right.
Just let's think about that for a moment.
Does this cause any problems at the end of the strings?
Because if i is 0, we're right at the beginning of the string.
What happens to i - 1? Well, we get -1.
Because Python allows us to index from the reverse of a string, that's fine.
That will be the last element of a string followed by the first one,
followed by the second one, which is just what we want.
Let's check the other end of the string.
If i is the last element of the string, then we've got a problem.
This element here is after the end of the string,
so we're going to get an error. What do we want it to be?
We want it to be the first element of the string.
And in order to do that, I'm going to use modulo n,
because i + 1 when we're at the end of the string i is n - 1.
Plus 1 gives me n, and n modulo n is 0--the first element of the string, which is what we want.
There we have our pattern.
We need to look up our pattern in the dictionary,
and we need to add whatever the character is that our central character changes to--
that i changes to--to our string.
Looking it up in patterns, I'll get that character.
Once it's run through the whole length of the string,
then I make my string equal to the new string and the loop continues.
Once that's done generations number of times, I return the new string.
That completes the procedure apart from a couple of typos.
That should say for i in range, and I managed to change my variable name here.
It should say pattern instead of patterns.