字幕列表 影片播放 列印英文字幕 What's going on? Everybody welcome to Part 12 of the Python three basics tutorial. Siri's In this video. We're going to learn a bounce, probably one more built in function and talk about how we conduce the the Diagonal victories now to do that. Actually, you know, if you especially with your hard coded there is really only two ways someone could win diagonally so hard coating wise. It's actually pretty easy to cover this one, so long as we're playing a three by three. But I want to maintain the ability for our tick tack toe to be scalable, too, any size, so it's gonna be a little more complex. But again, there's only two different ways in there. Have a pattern to them, so let's go ahead and get into it. So this one was for vertical winners. So all I'm gonna do, I'm just gonna commented out again later. Well, well, well, Adam altogether. But for now I want to work on a diagonal, so let's go ahead and add a diagonal win. Once again, Player, too, is getting more place. It's quite unfair. So So what are the two ways that someone could win? Well, they could either get this spot, this spot, this spot. And what would those be? Well, what would the indexes like, what with the game indexes be like this spot is 00 What's this one? This is 11 What's this? 1 to 2? Very simple, right. Uh, what about this one? This would be to 011 again and zero to notice a pattern. So, at least this way, parole, like the indexes, Are it just identical? You just increments up by land of game, right? Really easy on going the other way. You won. You just go up by Len of game, the other one, You go down starting at, you know, Len of game. Basically. Okay. So if in case that's not making any sense to you guys, you would just say if game 00 double equals and then we're just gonna go Boop and Boop and Boop. Um, and we would have a final check about not equaling zero. Um oh, I'm not worried about that right now. So anyway, uh, winner. And this would this would be 11 And then this would be too, too. So getting 001122 definite pattern there. And then again, this one would be, Let's say it could go either way, but, like 2011 or zero to run that winner. But if we take away that win, no winner, okay? And then what if we give, uh, won the victory in the other direction? Winner. Okay, so pretty cool so we could do that. But again, Yes. I want to be dynamic. I want to keep us, um, not hard coating things because it just it's just gonna bite you later. So So let's go ahead and, um, make something a little more dynamic. So first of all, how would we get, um how could we handle like, this case? So these are two separate cases, and they really are. One is going this way, and one is going this way. So it's handle for the first case here because this one is the easiest. So all we would really need to do is like, let's say we have a list of diagonals because basically the end of the day when we were doing verticals, we build a list and see if all the numbers are the same. We did horizontal. We didn't even have to build a list to see if there were all the same s. So when we do diagonals again, we're just gonna build a list and see if our they all the same. So So that's all we're doing again. So we're gonna say diagonals is a list, and then we're gonna say four and we're say, I x to be index in range of the land of our game. So that will be 012 As we've already seen. All we need to do is diag dot upend and then we append game. Not that whatever that was game and then that i x for index in i X again Really simple, then all we have to do. Well, let's just print, um Diag. I don't think you need to see the this thing, but basically, we're just going to use this logic again, and rather than check, it's going to be diag. Cz Okay, so I'm not gonna run that again. We're just going to see. Is it, um is it the same or not? No, it's not right. 011 But what if we make this one? Sure enough, 111 if we pass it into this little functionality we have here which, by the way, we've already started repeating. Uh, you know what that means? So anyways, um, we can see this. And when we run it through that sort of check logic, uh, it's gonna rain. True. We're gonna know we have a winner. So that way was actually really, really easy. And, um, so now let's do the other direction, because the other direction is going to be a little more challenging, but not much. So let's say we want to go the other way. So I'm gonna I could see what I want to do here. Um, part of me knows we've got so many commented out blocks. You don't have to comment this out. I'm just trying to do it. Just so the only thing you see on screen is the things that we're we're talking about. And by you, I mean me. So, uh, um, so we're gonna go the other way now, So how would we get, uh, you know, like basically one needs to go in order and we've already seen range Len game. That's how we can go in order How can we go in reverse order? Well, we basically just said it. So for, let's just say, um I don't know I in, uh Well, first it was range of Len of game. We can actually reverse that around so we can use another built in function called Reversed. Now, Prince, I Let's just run that real quick to 10 Done. Okay. Pretty cool. And again, as always, if you wanted to learn more about reversed, I mean, there's so much to learn about it. Uh, but if you wanted to, you could check it out. Also, I hopefully by this point, you've realized Oh, my God. There's a lot of really helpful built in functions. And again, you know, if you wanted to do reversed, can you think of a way you would do it yourself or range? I mean, my implementation of range would not be a cz efficient as their implementation or Len or whatever, but, um, if you wanted to do it, you could. And for most of these, I'll see if they're doing it here. Uh, they don't usually someone around here. I don't know. A lot of times you'll see the actual code, Um, that they used to actually produce the thing, but I don't see here anyways. There's been too much. I'm hunting for it. It's somewhere there. Or you could just read the source. Um, anyway, moving along. Also, some of these are like, see implementations. So I'm trying to think of some of them like, um, like pickle pickle and python 2.7. Um, is just like a regular python IQ implementation of pickle. And if you don't know what pickle is ah, we'll talk about it later. But it's just a way for you to, like save your AJ like is it allows you to serialize your object in like basically you can think of. It is it's a way to save an object like a file on your computer and load it back. So it's like already a python object when you load it in. Um, so in like Python, too, that was a pipe your python implementation if I'm not mistaken, and then there's a C pickle in Python to whereas in Python three, it's just the sea implementation. I think they figured like if someone is aware of the existence of sea pickle, they're always going to use he pickles. So why would we do this? So now there are sea implementations. I don't think reversed is one of them. But anyway, maybe it is. Okay, so let us continue along. So now we've got reversed range, Len. Games. So now what if we did the following? Like, what if we said so? We just iterated over. Reversed. What if we, um What if we did something like this? What if he calls equals, um, reversed the first? Why my typing this out even? Just copy, paste. Boom. Okay, so say that's columns. And then Rose equals range. Land game. So now we've got these two. They're not lists, but to minerals. One go 01 to the other one goes to 10 Um, Then what we could do is like, we could, um four. I don't know. I d x in rose. We could, uh, Prince I d x and then calls i d X. Right. That makes sense, I think. No. Oh, well, we pride hit another issue as well. Hold on. You okay? Yeah. So Okay, so we made our first mistake. Um, so let me bring that. Bring that back up, shall we? So let's say you hit this error and you're like What? I don't know what that means. Um, I'm just gonna quit python. Dang it. Um, like, how are you supposed to know what this means, right? That's not fair, is it? Well, let's go back to the professionals. Um, professional coders tool. Okay, I'm just searching literally the error. Let's go to the first result. That says python three. So that's us, anyways, who may be too much. Okay, I can't blah blah blah. Who cares what he's talking about? But he gets this air. The same errors we got scrolling on down, okay, Reversed function returns and it aerator not a sequence. Do you know what that means? Probably not. That's not very helpful to you. He's not wrong, but he's probably not helpful. So then we come back to here and we see the second best answer. I'm noticing a trend at like, the second answer is the most useful for for a beginner. Um, anyway, we can to see very clearly, you have to change it back to a list type. And then the first answer might start to make sense. It's not a list type. So that's what we have to do. We've got to convert it to a list. Okay, so we come back over here, um, we get rid of this error, and we go list Boone. Kind of weird. That range isn't angry. I bet we I mean, we're not trying to call an index on a range, though, because I don't think that would work. That shouldn't work. I actually don't know the answer. And I really kind of want to find out. Let's find out the answer. Can we call an index? So X range three. What if we say ex too, huh? That's pretty cool, I guess. Pry it. Just like next next to the index or something. I'm not really sure. Anyways, that works. You just can't do it on a reverse range or reversed anything, I guess. Okay, whatever, uh, moving along. Okay, so now that we fix that, everything works, and these are the reversed index is that we need, So we're feeling, you know, pretty good, but before remember, like, this isn't really the easiest thing for us to read. Um, and we definitely we do know about, like, index calls index. When someone's reading this, they pry have no freaking clue what you're talking about. The other thing we could say we could use a numerator, for example, and do kind of this very similar task that we're doing. But in this case, we're actually we're not trying to, uh, we're not trying to enumerate. We're not just trying because, like, in this case, we're just conveniently using I. D. X because it happens to be actually an index right, and so conveniently we are able to use it. But that's not actually what we're thinking we're trying to do. We're trying to combine these two things and then it arrayed over them. And that's actually a really common task in programming. And as you might suspect, there's a built in function for it. It is called zip. So what this is going to do is just gonna aggregate elements from each notice that each of the honorables so you could continue passing multiple intervals. So let's let me just quickly, um, why equals range? Uh, I don't want to do that. Uh, let's just a 210 zip x y Okay, so that just creates a zip object So how about four? I ends it, Tex. Why, uh, print, I Okay. You can see they've been zipped together. What if we did? Z equals 567 Uh, and then we just do a sequel. Zip X y z for I in a I think I did one too many spaces there. Print a, uh, four in a mention. Kind of surprised. Why did it say zip object there? I thought we would get away with that four eye in zip X Y z friends I There we go. So for a equals zip for I in a I'm kind of curious wide and that work I must be missing something obvious. Someone calm it down below. What the heck I'm missing. Anyway, My point of this is to show you that you can It's it's an infinite number. It doesn't have to just be too. But anyways, that's really what we're looking to do. And it's gonna make our code ah, lot more readable. So instead, what we we could say, is for, uh, Cole row in zip calls, Rose, rent, call, Rose. Boom. Okay, same outcome. Also, I suspect we could get away with not needing to convert that to a list anymore. Sure enough. Okay, so that looks a lot better also. Okay, so I'm gonna remove two lines here. You don't have to do this.