Fun with math and Rust


English (Auto-generated)

it is heads. Once again today we're gonna be doing a random math related problem I found. So basically you just want to break apart a number into like, okay, so we have 70,304 70,000 plus 300 plus four. Each place in the decimal is just getting added together. And if there's nothing there, we skip it out. So 42 is 40 plus 202 is 100 plus two etcetera etcetera. So it seems pretty straightforward. The only reason I'm sharing it actually is because I was thinking, hey, you know, how would I do this in Rust? Seems like an interesting problem. I was gonna do it on my own and that made me think I should show you guys the problem. So and there are cats going crazy outside and some dogs barking and my windows open. I have allergies like crazy here. They moved to an area where I can breathe. But anyway, so Sorry, my apologies. So we see that we have a number and we're gonna give it a string. So the first thing I'm gonna look at is us to convert number to string. I'm gonna go with that. I don't remember how to do it. Yeah, I noticed something like that. Yeah, it's two string. Sorry, my apologies. I'm over here having an allergy attack every time I talk to you guys because I start talking, I'm right by this window. But uh let's get back to work. So the first thing we're gonna do, so we're gonna be looping through this as a string. So I'm gonna go come and go through the character. Right? When we get like the seven like over here we have it we're at 70,000. Right? So probably 70,000 plus blah blah. So I want that seven character. I'll just multiply it by whatever tens place for you. Right? So this is gonna be the 10th place. The ones place will multiply that by 10 plus. Multiply that by one blah blah blah. Okay so I could probably do this using map right? Like I'd probably say like and dot to string you know dot chars dot map. You can do that magazine returning the wrong thing. But and then here I can say, you know, use an anonymous function. Say for each character. All right, there we go for each character and I forgot how to do anonymous functions and rust. No, it's closures. There we go. Okay so now I want I'm gonna want I'm gonna have a character and I'm gonna want to turn it to an ent right? Because I'm gonna I'm gonna have like the character five now. So I'll just say rust character number And yeah two digit. Beautiful. So how do I do that. Just say yeah two digit. Easy peasy. Let's say C. 0.2 digit. Okay and then I will take those and collect them. I'm gonna collect them into a vector. Mhm. Yeah it's gonna be like what do I collect it into um In reality I wanted to collect into a string but let's see if it'll be mad. You don't have to be like this like let s which is a string I guess. I could do like a vector Vector of U. 32's maybe. Mhm. Mhm. Was mad saying hey you can't build this thing out of options so it's an aerator of options. Okay well I could probably extend okay like that. Okay. Trying to have a bunch of options I guess because it's saying hey what if this can't convert it as soon as we're gonna try to convert it to a digit and it's not gonna be something that can be converted to a digital maybe some other kind of character. So that makes sense. Um Let's see how we get a string of that. Yeah. Well okay so I get them here, I convert it to a digit but I actually need to convert it to uh you know what, I'm just gonna do a for loop. Okay. Okay I don't need to track where I am and like the one place or whatever. Someone would have like rust loop with index on an operator. But I also want the current index stack overflow is unavailable. It's gonna be a lot of people freaking out over that but I can code the rest of it in the meantime, hopefully way way back machine isn't down to. So I'll say let like digits whatever. Yeah equal. And just create a new vector. It's gonna be immutable cause we're gonna be pushing to the right okay. Oh a new Marie, that's it. Okay well why is it saying this? Like why can't we just gonna say it? Er Okay I think we can just do it like this. So I'm gonna say for I call it index index. Did you? Okay In N two string. Dot charge might as well map and convert multi digits now. It's a map. Take your char char. Oh but you know what? I can enumerate it right here. Oh my gosh, I'm a genius. Sorry and I can reverse it first. That way the uh reversing it. The index will also be the power of 10 that I want to multiply by. Okay so yeah okay I'll get that. I'll say C will be si 0.2. Just say unwrap to the power of which I believe in do this a little differently now because now this is gonna be the character to the to the index and then the character and Mhm. And it's mad that chars can't be reversed. Okay let's look that up, pressed charges reversed. It's red, that's fine. I thought rev kim now this is not another turn type which actually makes a lot of sense. So almost there guys. Okay so we let's see we unwrap it um let me convert it to a digit. No murder two digit Base 10 and that may or may not work. So we have to say dot and rap. And from here we can say God the power of I mean that would be times 10 to the power of whatever our indexes. Beautiful. And now we have an error And saying it takes two distinct arguments Alright because it should take a single couple and just be structured like that. It's not knowing like is this 10 1 of the tens that has ability to power? Sure definitely know it's kind of a high Q. Dude, I tell you basically. Yes. Bottom huh chinese each other on your line. And it's funny because you wanna make these dancers so it gets put on its own line in order to do that. Right? And then you end up there's way more lines of code than you would have had. If you could just run this big epic loop. But no biggie. So I might want to put this up there on that first line so like blah blah blah blah blah time. See Yeah mm my apologies. Okay, everything's gonna be mad at about this point is I'm not returning the right value so I have this map and what I'm gonna do to it is join it, join it into a string saying you can't join a map. Probably what I have to do is convert it into a vector or something depressed. Map, join. Okay okay I'll collect it into a vector. So say dot collect. Okay vector of whatever is in there. I don't even remember anymore. What have I converted into U. 30 two's Listen Yeah, that's right, yeah, yeah, it's mad at me for some reason. Alright, because I'm missing a There we go. Okay, we are almost there everyone. So it's madness. Of course, because we're still in our returning collection, collecting into a vector when we join that vector with class. Oh, so it's saying, yeah, I can totally do this, but it has a trait bound that's not satisfied. So, let's see what's going on with that. Okay, check it out. Chance. That's right. £3. Seeing complicated. Quick, wow. Okay. Some crazy rebound. Sounds a little bit about my pig right here and it's always just create a new line of code. But who must do that. So it's a method of the generator. You can actually implement that trade, does not implement that trade. Mm Yeah. Okay. Oh, that's beautiful moments ever had this happen to them. Ever in the entire history committee. Okay. Looks like this person doing something similar though. That's where they will let me collect it like. Mhm. Mhm. Mhm. Maybe we just try guessing a little bit. I definitely didn't know. Okay, that's chris, let me do that. Yeah, this is totally reasonable. I just wanna see what, what type of sees this as. Yeah, Okay. That's what saying is struck of those things. Okay, hmm. I'm stuck on this sucks. Doesn't happen that often. Mhm. Mm hm. So is there to applicable, cloned functions cloned and cloned does not apply. It's only available for that, which is not a reference. It could apply. It's pretty wild. Let's see. Can I call method? Because isn't this exactly what being developers like just going through these things for a billion years us? I'm confused about the is that what I'm joining it? The following trade bounds were not satisfied. Oh, you know what? Okay. Have an idea. Mhm. No, totally not happy about that. Just thinking because of that little like you, a situation seems like it wants to be a slice. Yeah. Yeah. Two dot copy dot combinations dot collect into a vector is pretty wild. Yeah. Just say let the equal that. Get rid of the joint and see if it gets mad. It's willing to let me do that V dot going, you know? Yeah, Still superman. So let's see what's actually going on. Rust, join vector. The same air as me. Beautiful. Oh, I can join it. Just, I can just collect it into a string. Wait A. 2nd. Wait 1 2nd. Mhm. Yeah, I guess the idea of taking that. Yeah. Yeah. Because how would you do that? Like it doesn't even make sense. Oh, I get it now. I get it, you guys. This is so hard. The problem saying it wants me to join and saying, hey, I can't join these because these are numbers. So I joined numbers into a string. So after I converted into a digit and multiply it by all of that. Now I've got the right numbers in this vector, right? That's when I need to map through the vector I've created, you can put them all back into strings and then join those strings here. Yeah. Okay. So let's do it. It's wild. So here on the map again, I'm gonna be like yeah I'll take that like you know number I guess we'll call it n. You say en dot to string. Okay. And now it won't let me because I have to collect it now, I should be able to collect it into a stream. Uh huh. Okay. I think I'm done. You're still mad. Alright let's be equal. That's gonna return it. And I think it's satisfied everyone. Let's find out. Oh no it's definitely not gonna be satisfied because I'm gonna be including zero still here. So the only thing I gotta do is filter it and just say hey it's the it's the I I want to do this before I do the other map, there's always a point of converting which is zero is too strange. So as I said just be like hey give me that N. And make sure the filter is that n. Does not equal zero filter on all the other stuff. I don't want any unequal zeroes up in here. Okay it's mad because saying this is a reference so I'll just the reference it. Okay I guess I mean that should work. I'm pretty sure like we didn't test it at any point while running it but I think that should work. I've been saving this kind of Okay. Set out. Okay well that that did not work. I mean clearly So we got to 10. It's supposed to be two plus 10 is definitely the wrong answer. So for one would have to reverse it right here. Right? But Mine is coming out is to 10. It's crazy supposed to be all right. That's supposed to be two plus 10. Right okay. Who faces wild? Because I got ready to join. Yeah we got rid of it. I do have to join them and now that they're going to be converted to a string. Mhm. I don't want to collect into a single string. I want to enjoin This map of strings. So I'm gonna collect it into a vector of strings. Right? That's that's gonna collect it into a vector of U. 32. But you can't join them together with string so I'm gonna collect it. It will be a vector. Now it'll be a vector of strings. Well then join using this Beautiful plus situation. You've got right here and now will still be backwards I guess so I guess I'm just reverse the vector. But let me try this first. This should say two plus 10 now. Yes. Now we have two plus 10 and we should have 10 plus two in order to fix that. We literally just can reverse this vector we made Oh, uh not an operator, it is. Now, now we have a reverse generator, which I then need to collect. Mhm Yeah, I guess I can do this earlier. I don't have to recollect it, I gotta do it right here like that. I'm not gonna worry about it, I'm gonna say collected as that reverse it. I totally got lost on what I was doing right now. Making an operator reverse it re collected is exactly the same thing. The most pointless thing I've ever done. It's mad. But see, no. Oh, because now it's a slice. I don't know if that's because I reversed it or what? Which would be unfortunate, but let's see, it's like, we don't want you collecting this into a bunch of references to strings. Okay. That really doesn't matter. That references to strings. There's anything I can do about that. Mhm. Mhm. Versus an operator's direction? I don't really like. There you go. Okay, I get confused about when we use generator and into generator, but that totally fixed it. So, let's see if it's happy now. Okay, and tapping up attempt. Oh, so attempted with overflow. That's very interesting. Probably what's going on is I'm using unsigned, I want them to be signed. I had to guess. Oh unsigned, 64, Yep. So it's giving me unsigned 64 bit integers. And I'm saying that these are all time 32 bit and that makes it mad at me. The reason it's mad at me basically is that It's gonna give me the 64 bit things right? And I'm saying oh yeah return I use 32. Okay. But the digit isn't really the issue here. We converted two digit. That's fine. But yeah so when we multiply this number were saying oh yeah these are all U- 32. But no it's actually gonna be this huge number since you 64 and that was breaking it. All right. I knew about that. You want to change any of this stuff? 10 definitely isn't the problem. So I guess that's what I can say to you. 32. That one doesn't really matter. And this one actually can be you 322. I just want how give me a bigger number Breast X one in use 64 instead of using two. Okay. So I guess converting this to use it before actually would in theory because And how this needs to be a U- 64 as well and Mhm And U- 64 does not have a 2-digit method. Okay this is interesting. So 02 digit. Wait a sec. I am so confused. This is like the hardest problem I've ever solved. I just went into this one live. This is 30 minutes. People who are still following me all of the true believers here. Mhm Let's see. So I'm converting it two in digit. Let's see. Rust convert you 64. Okay, so I have my character C is the one who convert to a digit. That's what I gotta take care of right there. See needs to be converted to a digit which will be unwrapped. Beautiful. And here expects Au 64 at one As you 64 maybe right there, definitely not. Probably the whole thing. Well, multiply this avenue 64 and now it says it's happy. Let's see what really happens. Go straight to the pen some crazy and it passed all of them beautiful to win some points. And that's what it's all about. Right, We're on the internet to win points. Not kidding. That's a good point. I could have converted these using format. That would have been more intelligent way to do that. Yeah, there you go. That's beautiful. I don't like how he uses the kind of generic factor right there, collectively make it do that, wow. I hope that was educational for you. That was certainly educational for me. I will talk to you all later because I'm going to sleep good night and take some allergy meds or something. I don't know what I'm gonna do. I'll talk to you later. Goodbye
167 Views 0 Likes 0 Comments

Doing a random math problem from Codewars...with Rust!

Leave a comment (supports markdown format)