Python Challenges: To square root or not to square root
Transcript
English (Auto-generated)
um today we're going to be doing another python challenge. And this python challenge is once again very a very simple challenge but it involves a bit more math. So we'll see how we're gonna do that. Okay so this problem is that we're giving a list or if you're familiar with javascript and we're going to and it's gonna contain a bunch of different numbers. And basically we're going to check if those numbers are square writable or not square irritable. And if they are a perfect square, we're going to return the square root of that number. And if it's not a perfect square we're just gonna return that number squared. So I hope that makes sense. Um So the first thing we're gonna do, I already have here import math because we're gonna be using uh a tool that is used by that. And um so let's get into it. And so for the first thing we're gonna do is create a new list. So we can depend are the numbers that we're iterating into that new list. Um And we're just gonna make it an empty list for now and we're gonna create a for loop so we can read through the current list that contains all the So we're gonna do four number in that list. We're gonna check if it is a square root or not and how are we going to check that. What we're gonna do is we're gonna do if mascot squirt which is um the square root of that number. And we're gonna do math dot square number is greater than the inter version of math squirt number then it must mean right. It is not it is not a square root a perfect square. And how does that work? You might ask. So I'm gonna show you guys really quick an example of that which is basically that if we have for example math squirt um to it's in return one die a bunch of different things and if we put into it math dot squirt two gives us just one like it basically grounds to the lowest introduction. So in other words it kind of cuts off the decimal part and if it even has a decimal part of that and that shows that it's not it's not a square, a perfect square. So that's how we know but if it's profitable or not. So here we're just gonna put list new list dot append. Usually what I would do is plus equals. But since we're adding in teacher and the teacher and then into prison in trouble like that where does it put that append? And what we're going to append remember if it's not a square root we want to return that number squared. So what we're gonna do here is the inversion of sad number to the second power or squared and else as in if it is a perfect square we're going to do new list. Not a panic. And what are we gonna do if it is a perfect score? We're gonna return the square root of that number. So we're going to do math. The squirt um number. And remember that once the end version of that. Okay. And then there were just going to return the new list just like that. Yeah. So now we're gonna go ahead and test it out. See how that works. Okay. We're gonna go here into our criminal and type python, three of them in the name of our row. Just like always two square root or not to that time. Oh I'm going to print here so now outside of it we're gonna print and then we're gonna put the name over function. What is it again? Square square root or No. I tried to make it as short as possible. That's where we're gonna know. And then we're gonna put our list and let's put 2497 to non square writable numbers and two perfect squares. So let's see what it gives us there. And as you can see the two since it is not not a square writable number, it returns the square version of two as in four. And for four since it is a perfect square, it returns the square root of that number two. So and it does the same thing with three and with 49. So Perfect. Okay. The only thing that you guys have to re factor in this situation is that we're repeating math. That's word number three times in this situation. So I'm going to create a temporary variable to where we started math, not squirt number because we I don't want to repeat ourselves too much when writing our code already an elegant rhythm because it just doesn't end up as clean as it could be. So the temporary variable, we're just going to call it, let's say the root of a number or no. Um let's just leave it up numb and I'm gonna make it math got Okay, perfect. And now we're just gonna replace all that with just squirt number and right here we use it again and we're just gonna put squirt of them and same thing here of, you know. Okay, that is perfect. Now we're just gonna check if it works properly and it does, it looks exactly the same and that is kind of a revived version, even though it looks a little bit more company, it's a lot more readable and cleaner in my opinion. So yeah. Um thank you so much for tuning in on this code cast and I'll see you guys on the next one. If you guys found a different way to solve is a cleaner version in a different way or you're just re factored in a different way, Let me know and you can go through that together. Thank you so much and see you guys