Color tt Transcript
[00:00:01]
I’m going to show you how to do the color T’s problem. So what we can see here
is Bit is supposed to look up or identify three colors in a row and then go paint
three T’s in a row.
Start visual description. Screen reads color_tt.py. Two grids are shown as the output for the exercise. End visual description.
[00:00:16]
You can see that we’re going to need the ability to detect the color and
remember it using the get color function and then we’re going to need a function
that allows us to pass that color in, in addition to bit, right. A multi-parameter
function.
[00:00:35]
So let’s take a look at what we have. We have some starting code. So here, in
color TT, we need to implement the run method but there’s some others that are
given to us. And I’ve collapsed them down so that we can’t see all of their details
right now, just their signatures.
[00:00:56]
But if I hover over it, I can see the little explanation that’s in there, right. So at the
bottom of this drop down, you can see go until blocked in front, right. That’s
what go does. We’re familiar with that. We don’t have to worry about the
implementation of it.
[00:01:12]
How does go accomplish moving into a blocked in front? We just know that if I
call go, it moves into blocked in front. Much like the move or the paint or the
turn methods on bit, we don’t really care how they work, we just care that they
work. And they do, and so we use them, right.
[00:01:33]
So here’s a new function right here that just lets us go. We can say go to start.
What does that mean? It means Bit starts anywhere on the board facing right
and ends in the bottom left corner facing right. We can draw that out to make
sure we understand the gist of it, right. If I’ve got some grid somewhere and Bit is
anywhere facing right, he will end in the bottom left facing right. So go to start,
right. Return back to where he is.
[00:02:04]
And, once again, we don’t have to worry about how that’s implemented. We’ll
look at it in a minute, it’ll be interesting to see. But right now we’re going to just
take it at face value that it does work. That it will get Bit back to start.
[00:02:21]
What about paint T? Right, it paints a T. And specifically, what does that mean? It
means it starts in the bottom left of the three by three box and ends outside the
bottom right of the three by three box. Let’s draw that out.
[00:02:37]
Okay, so if I’ve got a little three by three grid right, it says Bit starts right here in
the bottom left of a three by three and ends right here just outside the bottom
right of a three by three. If we come back and look at our drawing, we can see
these T’s reside in a three by three space, right. So the T here, Bit starts here and
then it’s going to draw in those positions but end right there.
Start visual description. Instructor sketches out a grid to show the thought process for the exercise. End visual description.
[00:03:13]
How? Details. We don’t care. What we care [about] is we’ve been given a
function that says it will do this and we can use that now to solve our problem.
Let’s come back and look at our code for a second.
[00:03:28]
So we have these at our disposal, we just need to implement run, right. How do
you put all of these things together in order to solve the problem? So we come
back and look at the drawing. There’s two initial ideas, right. We need to get the
colors, the specific sequence of colors, and then we need to draw the T’s.
[00:03:52]
So true to form, why don’t we just write some functions that do these two things
and put them together right? So we can come down here and we can say get the
colors bit, paint the T’s. Paint the T’s bit.
[00:04:15]
Okay, so if we do those things this should work. But there’s a little bit of
information though that needs to go between those two, right? In the process of
getting the colors, I need to return the information of three different colors.
[00:04:29]
And then in the process of coloring the tees I need to give it information about
what the three colors are that it should paint. Right. So this thing. Let’s say, you
know, if it were to return one color we’d say like color equals get the color.
[00:04:47]
In this case, it’s going to return three. Turns out you can just do this, right. Color
one, color two, color three equals get the colors. And get the colors is going to
return three colors.
[00:05:00]
Now, paint the T then, we need to pass that information in: color one, color two,
color three. And now it can go paint those T’s. Great!
[00:05:13]
So now let’s implement just for a minute these get the colors and see where that
gets us and what we need. So we come in here, let’s say quick function get the
colors. Right, let’s document that. So Bit starts on the first color and it gets three
colors in a row. You know, gets three colors in a row and returns them.
Start visual description. Instructor types the following code:
@Bit.worlds(‘color_tt’, ‘color_tt2’)
def run(bit):
color1, color2, color3 = get_the_colors (bit)
paint_the_ttt(bit, color1, color2, color3)
End visual description.
[00:05:53]
Okay, great! So looking back at our picture here. We’re starting here, we want to
get this color, move, get this color, move, get that color, and then return that bit.
Right, so let’s say first equals bit dot get color. bit dot move second equals bit dot
get color bit dot move third equals bit dot get color return first, second, third.
[00:06:21]
We got the colors. They’re all set, right. How could we test that this has been
successful? We could throw in a couple snapshots if we wanted, that have the
names of these colors, right. So if we said bit dot snapshot first, first is a quote
blue or red or green. It’s a string. And snapshot takes the same thing. It takes a
string. So we could throw that in there.
[00:07:00]
We could do the same thing right here: bit dot snapshot second bit dot snapshot
third and now we can step through the snapshots and actually see the colors that
we have.
[00:07:13]
Python will be unhappy if we don’t implement this first. I guess technically it will
let us run, but here we’ll just pass, right, not worry about that for a second. Let’s
just see what we have so far. So let’s run it. There we go.
Start visual description. Instructor types the following code:
def get_the_colors(bit):
“””Bit starts on the first color, gets three colors in a row and returns them.”””
first bit.get_color()
bit.snapshot (first)
bit.move()
second bit.get_color()
bit.snapshot (second) bit.move()
third bit.get_color()
bit.snapshot (third)
return first, second, third
End visual description.
[00:07:43]
Okay, we can see, let’s just jump right to the very beginning. Okay; snapshot red,
snapshot green, snapshot blue, okay. It figured out those three colors, so that
part was working. What we do see is those are supposed to be white once we’ve
picked up the color. So let’s add that in real quick and then we’ll figure out the
next step.
[00:08:04]
Oh wrong button. Close that out, okay. So we get the color and then bit dot
erase. We clear it out. I’m going to copy that line, put it in after each of our get
colors, run that again. There we go, right. So we’ve grabbed those three colors,
but we can see here that we just grabbed red we just grabbed green we just
grabbed blue. Perfect.
[00:08:39]
Now we just need to draw the T’s. So from here, right, Bit ended where the blue
square would be. Now we need to get down to the corner and draw some T’s.
Well, we have a function for taking Bit from any position and getting it to start,
right? That was one of the ones that was given to us right here: go to start.
Anywhere on the board facing right and ends in the bottom left facing right,
perfect.
[00:09:05]
That’s where we want to be before we start painting T’s, right. We could make
that part of paint the T’s or we could consider that glue code. Go to start bit. Just
throw that in there. And now Bit’s at the start and paint the T’s is ready to go.
Right, so we could document that in here. We can say Bit starts in bottom left
corner and paints three T in a row.
Start visual description. Instructor adds go_to_start(bit) to go function. End visual description.
[00:09:40]
And then we can see that Bit’s supposed to end just outside the three by three
box of the last T. Which, is convenient because that’s what the paint the T
function is going to do. It’s going to drop Bit just outside.
[00:09:56]
So as I paint the T here and our paint… with our paint the T function, right, Bit
starts here, paints this T, ends here which is just exactly where the next one is
supposed to go. Okay, so we can see how we just need to paint a T, paint a T, and
paint a T.
[00:10:14]
So let’s use that paint T function that we’re given. Paint T bit and then we’ll give
it the first color. And we’ll do the second one with the second color and paint T
bit color three with the third color. We’ll just be using those. And that looks
promising, that should do it. So, we’ll run it. Yay! Compare correct,
congratulations.
[00:10:48]
Let’s go to the beginning here and let’s step through our snapshots and see this
all coming together. Right, so we snapshot red, we snapshot green, we snapshot
blue, then we jump. Oh it turns out that uh we have a snapshot every time we
paint a T.
Start visual description. Instructor types the following code:
def paint_the_ttt(bit, color1, color2, color3):
Bit starts in bottom-left corner and paints three T in a row.
paint_t(bit, color1)
paint_t(bit, color2)
paint_t(bit, color3)
Instructor runs code to show the three different colored T’s outputted on the grid.
End visual description.
[00:11:05]
So that means that some implementation under paint T has a snapshot in it. Still
haven’t looked at that implementation but it’s in there. We can jump again, see
the next T is painted, jump again, see the last T is painted and we’re all correct.
Let’s check the other map. Same process, but you’ll notice the colors are
different, right. So we grab blue, we grab red, we grab green, we paint blue, we
paint red, and then we paint green.
[00:11:35]
And with that, that’s how you would you can break down this problem. Now let’s
go back and look at some of the implementations here just because it’s
interesting. Go until blocked in front, that’s not very much of a surprise right. Go
to start. What does it do? Well it turns around, it goes, it turns left to face down,
it goes, and then it turns left.
[00:11:59]
It’s just a simple maneuver. We’re using that go function. Great. How about paint
T? It’s just a lot of moves and paints with a little snapshot at the end, right. The T
has been painted. The key idea to decomposition is that we can take functions
that are given to us and put the problem, solve the problem using those bigger
pieces.
[00:12:29]
Now not always are those bigger pieces available. In fact, most of the time we
have to create them. But the process is the same, so if I were to give you the
same thing with none of the starter code then you would go through that same
process of thinking: well what words do I wish I had that would let me solve this
problem?
[00:12:52]
Now let’s just pretend those words exist. We’ll define them with some doc string
and what exactly it’s going to do, use the word as if it has already been defined to
solve the problem, and then we’ll go back and actually tell Python the details
about how to fulfill its intent. Practice that process. Get really good at making up
words and you’ll be very successful in programming. And I wish you all the best.
Good luck!