All right, it's time for yet another
difficult coding exercise.
So this is a difficult challenge.
What you're going to do is you're going to pretend
to be a pirate.
What do I mean by that?
We're going to use what we've learned about lists
and especially nested lists
in order to create a Treasure Map.
So we've created a Treasure Map by creating three lists
which I've called line1, line2, and line3,
and I've combined them into a nested list
by using the code line four,
where I said, map equals
another list that contains three lists.
Your job is to take an input in a similar format
to how you see maps are formatted,
where we can take a letter and a number
and then use those letters and numbers
to access a particular location in our list
and convert the entry into an X
to mark a spot on our map where we've buried our treasure.
Have a read through the instructions carefully,
figure out how this is supposed to work,
and then write your code in main.py
between the two sections that I've already written for you.
So we've got a print statement
that prints out all three lines,
and you shouldn't change any of the existing code
in order to complete this challenge.
It's a difficult one.
Have a think about it.
Think about the Example Inputs and Example Outputs
in the instructions and give this a go.
All right.
I did warn you it's a difficult one,
but there's no shame in checking the solution,
especially if you've given it a good go.
The first thing that our code does
is to grab hold of the first letter that's in the input.
In this case, it is B in the example input.
And what we're going to do is we're going to use position
which takes the entire input,
get the letter at the first index,
so, position[0]
and now we have that first letter from the input.
Now the next thing we're going to do
is we're going to turn this letter into lowercase
so that we can compare it against a comparable.
I've used the .lower() on line eight
in order to lowercase the input letter
just in case a user used the capital.
And then on line nine, I've created a list
that represents the three possible letters.
We've got a, b, and c.
Now, you could do it the other way
and make it go uppercase as well,
but everything has to be uniform
in order to make sure that the user input
doesn't matter if they put in lowercase or uppercase,
our code will still work and can compare it
against the values in this list of a, b, c.
In order to do the comparison,
we're using a method that comes from the Python list
called index.
So the way that we do this
is we take the name of the list, which is a, b, c,
and then we write .index,
and then in the parentheses of that method,
we pass in whatever it is we want to check
that exists in the list.
If the letter was b, as in this case in our input,
then abc.index(b) is going to give us a number,
it's going to give us 1,
because remember lists start from 0.
So a is at index 0, b is at 1, c is at 2.
So now letter_index is 1,
and we can work with it in order to get hold of the item
in our map that we want to change.
This part is a little bit difficult to conceptualize,
but you can try thinking about this code,
printing out what the values are
and making sure you understand.
The next part is getting hold of the number_index
which is a lot easier
because we can simply take the input
which comes in through the variable position,
get the value at index 1
which is the second digit or letter in the input,
and in this case, it's the number 3.
And in our map
because we have a list and remember lists start from 0,
s o actually position 3 is equivalent to index 2.
So we always have to subtract 1 in order to get the index
because it starts counting from 0.
So now we have the number_index.
Finally, we can bring up our nested list, our map,
and put in both of these indices
in order to change the item at that position
to the letter X.
The thing that might be a little bit confusing here
is the first part is actually the number_index
because remember we said that with nested lists
we go from outside to in.
And what that means is we've got a list of lists.
So if you look at line four, our outer list is our map
with line1, line2, line3,
and the part of our input that determines
which of those lines we're going to pick out
is actually the number, not the letter.
So that's why the first index that goes in
is the number_index.
And then once we've gotten onto the correct
line1, line2, or line3,
then we use the letter_index
in order to pick out the right element
in that nested inner list.
This again, is a little bit mind blowing.
It's difficult to get your head around,
but it's okay if you just try it a few times,
try the code, try some different combinations
and eventually you will get around
to this programmer's way of thinking
which is a little bit different
to what you might be used to.
Be patient with yourself.
If you have any changes you need to make,
go back to the previous slide and make it now.