Part II: a HARD algo in Rust! (redo)


English (Auto-generated)

It is his. So this is a redo of part two because I I had it focused on the wrong window. So we should be looking at the code now. Um here's where we left off in part 1? We have rotate beck that still needs to be written and we have the snail function that looks like it should work the basic logic of the algorithm. It's all there. We just need a way to way to rotate a matrix. So how are we gonna do that? Okay well basically what we're gonna do is we're gonna say hey start over here. So we want this to rotate it, we want 36 and nine to be the new top, this to be the new middle and this will be the new bottom. So we should be starting at zero row zero. We're gonna start at three, that's row zero. So we're gonna go 012. So we just need to loop from the length of the of the vector. So first well say another we can put this is we want the height and the length. This might not be the same, right? Like this is a square. But once we chop this part off this top one now it would be you know, not a square, be some other kind of erecting. In other words this should rotate two dimensional vectors matrices, you might say even if they're not perfect squares. Yeah, anyway and it will be a vector of vectors. Type I 32. I gotta correct that as well. Okay we're not returning anything. Of course. So let's see. So we need to start at 01 and two. So say four, I am zero up until the length of the vector. Yes. How many rows there are going to start at the first row and go on and on and on. And I'm just gonna return a quick vector with a vector in it. Just to make it stop bothering me about return types. There we go. Then we need to do the last ones first and then these ones and then zero. So we're gonna go from 2 to 1 to zero. This is the length of the row. Okay so you might say if and remember we want to start at zero dot, so we need to start at two and then go 01. So in other words this loop needs to be inside. I just realized that because think about it, we're yeah we're gonna start at zero but we're not gonna go zero. Then here then here we're gonna start it to then go blah blah blah, then one blah blah blah, then zero blah blah. So we're gonna start with the length of the road. So let's get the height and length first. So the height is just the length, how many roads there are? Okay if the height is uh is zero by the way, I think we could just say go ahead and return an empty vek they just returned nothing. Right? Yeah, okay man, I gotta say there we go. If there is something in there we can say we can say that that's how wide is it? We can say that the width equals um Just get the first row. That should all be the same. We'll get the length of one of the rose, presumably the rows are all the same. We in theory could rotate a vector that has variable length Rose. Right. Just be a little more work. But for now we'll assume that I mean for the context of this toy problem will always be rectangular at least. I mean it'll always give us squares and chopping off parts of squares will only give us more rectangles. Okay. Who? Yes I'm just making sure I was just making sure I'm sharing the right screen because I had that problem with the last verse of the video. So now I'm all paranoid. Okay so now what I was saying is to have the height and the width is that we want to start with the width because we're gonna say okay go to the last one and then do 012 and go to this 1012. So the 012 is our inner loop. Our outer loop is one says go out here in the middle and zero. So we're gonna loop using but it will be like well you'll see okay so we're gonna we're gonna start we're gonna go from zero up to the width and we're gonna start at the end so we'll reverse that. Okay? And that will give us our column, right? Sorry. And so that's gonna give us a call. But first, in fact, I'll just call it column. Then we'll go ahead and say for a row in Between zero a hike and now we have access to these things, right? So we're gonna want a new vector. So I'll call it, you know, rotated right? We're gonna want this new thing where we can put the results, we're getting, create a new vector there And we'll start building a road. So for each time we go through all three of these will add this to a new road. So we'll create a row. That's also gonna be a vector. This will just be a vector of I-30 queues type and that will equal another new vector. So many vectors going around right now, I'm gonna call this new row. We'll add this to our new roast will say dot push. And we'll say out of the old vector, it will be we'll get the row first in the column. And it's mad at me for some reason, who knows why I think it actually pushes a reference. That's actually not why. How interesting. I can't. It's not mutable. And this should be immutable as well. There we go. And then I'll just add this to my new rotated matrix, I'll just add in this new new row. Beautiful. And now at the end we can return rotated, wow. I think that's it. Like I'm pretty sure that this is legit. So how was playing with wrestling's earlier recommended that another video. It's really fun anyway, so we will say cargo run. Yeah. And we just wait a few years. I think I added some stuff to my cargo because I was doing some other coding using the same file in my free time. It's too lazy to start something new And it didn't work. We get 12345678 Blah Blah Blah. So for scaling that is not what we would expect. Very interesting. Very interesting. So Oh right because we rotate it but we don't ever change it. So we want to say yeah the matrix equals the rotated version of it because we we return it but we're not changing the matrix. So we have to do that. There we go. Moment of moment of truth. It's gonna happen. Come on, come on. I love this moment encoding. Hopefully like something didn't freeze or something. Like I was using my notes app earlier and it just completely froze out of nowhere. It's gonna go maybe it's running forever. Like oh right because I have a loop this loop here. It's probably going on forever. That's definitely in fact I would say that's very likely the issue. So here's what we can do. We can convert square to a vector and then rotate it. So I'll just go ahead and say no rotate back. What am I gonna give it? I'm gonna give it square dot to back and yeah you should be happy with that mismatched types because I think it wants a reference. There you go go run And did it work gave me 369 on the first row. 258 and 147. Yeah there you go. So it's rotating it correctly. That's a very good sign. All that means is that for whatever reason So our our snail algorithm is the one that has the problem here. Okay well that's good enough so let's see what let's debug snail real quick and then we'll be all done. Um The third video which is already out is the one where we're already done and we go ahead and start using traits and generics and all these fancy rust features. So yeah let's see what's wrong here. So this this loop I'm pretty sure is just going on forever. So I'm just gonna go ahead and say um I'm just gonna go ahead and say let's print some data and see what's going on. Print based debugging much maligned but very useful practice that all software engineers secretly used to build the whole world's software systems. Okay start to run I gotta get rid of this these packages but okay so it is empty. The problem is it's saying so I see exactly what the problem is. So saying is the length zero And no there is one empty array inside of it. So let's see what happens when I do that and that looks right. But let's just check it out because in five no, because now it's not gonna do the last one. Yeah, that's so annoying. Okay So in other words giving us 123 Then it goes down 69874 and then just doesn't give us that center one so we can go down here and then say like well let's see what's still in there. Right? So let's go ahead and print the entire matrix. Yeah Yeah, we see that. It's always a five minute. So mm if if okay, matrix length Is greater than zero equals one. Actually I guess it would only ever be one. And I'll say if now we know there's something in it. So I'll say if matrix zero dot ling it's definitely gonna be another. Matrix is greater than equal one as well man. I'll just go ahead and say, hey yeah put that in the snail ified Al fied dot push matrix 00. I should do the trick. There you go. And just out of curiosity we could see if this would this would still work or if this becomes brittle 1254 that's exactly what it should be great. So our code works. Congratulations us and I did it in almost the same amount of time as my other videos were. So I kept on brand. Yeah. All right, well, thanks a lot for your time. I'm gonna go ahead and end this stream. Um, I'll create a little series out of this because now they're gonna be out of order of my profile because I'm redoing this. But yeah, thanks a lot for your time. I'll talk to you again soon.
109 Views 0 Likes 0 Comments

This title is a mess! But our code is clean B)

Leave a comment (supports markdown format)