Solving a Codewars kata with Rust

Transcript

English (Auto-generated)

it is Hayes today we're going to do a problem from the website. Code wars dot com. I'm gonna be doing in rust. So if you look at my screen here you should see code wars And I also have the actual have an editor open with some code already but I'll get rid of that and we can write our code in here that you guys have access to. Alright, so let's copy over our code and put it in the old editor. Wonderful. You should be able to see it now in the editor in code cast. So the problem is Multiples of three or 5. So says basically find all the numbers, it's gonna give us a number called numb and we need to add up everything from zero to that number. That's a multiple of three or five. So if we list all the natural numbers below 10 there are multiples of three or five we get 356 and nine. Some of these is 23. So 23 would be the answer. If they passed us 10, finish the solutions that it returns to sum of all the multiples of three or five below the number passed in. Additionally, if the number is negative return zero for languages that do have them. Okay, I don't know what that means for languages that do have what negative numbers anyway, if the numbers are multiples of both three and five will only count at once. Great. So we're gonna need to keep track of a sum of some kind that we're gonna add that. The first thing we see is we're gonna loop through all these numbers. Let's go say for I and I will let copilot help us out a little there kind of got some things wrong. But yeah, we'll go through each of these. So we're gonna start from one and go up to numb and we'll say if I want to check if it's divisible by three or if it's divisible by five. So we'll say if I is divisible by three, by the way, we're using the modular operator here, this is the remainder operator. In other words, it tells you the remainder. So if the remainder of I divided by three is zero, there's no remainder. That means it's evenly divisible Or if it's divisible evenly by five, then what do we do? We're gonna add, add it to this summer, Right? So we'll make a mutable variable called some changes to zero immutable because we're gonna keep adding to it and we'll say some plus equals i under this condition. And then down here at the very end we are going to want to return the sum. Alright, let's go into I think return here because I'm mostly program in python in real life, I'm pretty much a new rest station myself. So I think it's fun to learn together, you know, maybe I'm not this great rust expert, but it's a sometimes I learn more, you know, seeing the way someone actually learned something rather than them presenting something, they're already a master at. Alright, alright, let's try it. Mhm. Okay. It is mad because let's see why is it bad? So I think that if I just put Oh right, right, right, so it looks for the last expression so I shouldn't have that chemical in there. Okay, passing the tests and there we go. That's it. Let's submit it and get those delicious internet points that we're all here for and done. And remember to market very satisfied because they give you another point for rating the kata anyway. And uh for those of you who aren't super familiar with code cast, sorry, podcast with code wars, you can look at other people's solutions on here. It's a great way to learn. So you see this person basically did the same thing as us, Right? I mean instead of plus equals, they use equals plus. They don't have the parentheses here. I think they make it a little clearer, they have some extra white space, this is the same algorithm. Okay then look at this person, they create an iterable like this using data, then they filter for just the ones that are divisible by three or five and then they sum them all together using this built in method. Beautiful. Beautiful. I think that's really elegant. In fact, I'm gonna upload it and you can look at other stuff, you can always find weird solutions here too. Sometimes you find solutions that are better that use best practices that were maybe a little like wiser from a software engineering standpoint than your own little clearer code. Sometimes there's a language feature you didn't know about and sometimes there's a solution that's just horrible, but it still makes you curious, how did they do that? And you learn about all kinds of cool features? That way, new ways to think it's a really great place to be. I think I often learn more coming here and seeing how other people solve the problem, I could solve, seeing how they did things differently. It's often more educational in the actual algorithmic part of the challenges. So anyway, this is a great learning resource. So for the rest of you rust learners out there, I hope this is something you can go to, something you can have fun with. I have been addicted to code wars For like four years now. Um so I hope I can share the joy of code words with all of you, thank you a lot for your time and I can't wait to see you next time. Goodbye
102 Views 1 Likes 0 Comments

Here's the link if you want to try it yourself: https://www.codewars.com/kata/514b92a657cdc65150000006/train/rust

Comment
Leave a comment (supports markdown format)