Write some Python code that defines a function called find_min(a,b) that returns the
smallest of the two given parameter values? Can some one help me with this please?
Thank you so much megha
def find_min(a, b):
return a if a < b else b
@ farhan787, I like your solution, but what if the 2 numbers are equal?
We can use this as well (compress python code)
@NicolasATC, in case of equal numbers, it’d go for the else case and returns b, since both are equal doesn’t matter which one do we return.
@farhan787, that totally depends on the problem you are trying to solve. So, analyzing the problem will dictate if you have to identify if 2 numbers are the same.
As I read on this quote:
A Good Programmer Is Someone Who Looks Both Ways Before Crossing A One-way Street.
– Doug Linde
def find_max(a, b):
if a == b:
return f"{a} is equal to {b}"
elif return a > b ? a : b
You get the point, the indentation isn’t coming out
@NicolasATC nice quote but nothing depends on the problem here, we simply have to return the minimum number, why to complicate this thing? Also, my function is handling all the cases assuming the parameters passed are numbers because I didn’t do any validation there.
If the function looks for the minimum between 2 numbers, then that’s what it should return. If you pass 5 and 5, and your code returns 5 as the smallest, then your code is not giving back the correct output, because 5 is not smaller than 5. In this case the 2 numbers are equal, so
But in the end it is your code, I was just trying to provide another point of view to your code. Some people accept it, some don’t.
All the best in your coding journey!
@farhan787’s function is the only function posted here that fulfills the requirement of returning one of the given parameter values.
If you pass 5 and 5 to a function what is the smallest value passed to that function? Well, it’s 5.
Since the text is a common programming excercise with a common function name we can assume that we should implement a Min()-function as seen in almost all standard libraries that behave exactly like this and returns the minimum of the given arguments.
Use code fences.
I am just a beginner here but i like @farhan787’s function better. In the end of the day, there is really no right or wrong function as long as you get the desired result. You shouldn’t care what happens under the hood of a function if you just want which number is the smaller from the two. When you call the print() function you don’t look under the hood and say well i don’t like the way they wrote it so it doesn’t meet my requirements.
From what i read and learned, if you’re writing a lot of repetitive wordings in your function, there is a better way to write it.
farhan is showing more of a more efficient and cleaner code.
The function name is find_min() and if you wanted to define what your function does it is to find the minimum of two numbers and he minimum of 5 and 5 is 5 right? if you wanted to find if numbers are equal then right a new function find_equal_num() or something.
just my 2 cents…
Thanks @SAM and edu for getting what I’ve done. @NicolasATC don’t take me wrong but I guess you’ve just started with programming, if you don’t believe me or my code then you can try min(5,5)
in Python and Math.min(5,5)
in JavaScript and std::min(5,5)
in C++. All will return 5, because that’s the right answer. Agreed that 5 < 5 is not true but in context of minimum number the answer would be 5.