+ 1
Intermediate Python - 17.2 Practice help - âMaking it workâ
The given code defined a function called my_min(), which takes two arguments and returns the smaller one. You need to improve the function, so that it can take any number of variables, so that the function call works. Please help! Thanks! âââââââ Current code: def my_min(x, y, *args): if x < y: return x else: return y print(*args) my_min(8, 13, 4, 42, 120, 7)
8 Answers
+ 4
This can be solved using the above mentioned defined function ie like here:
https://code.sololearn.com/c098X9UiGgQ1/?ref=app
+ 8
Zach Z ,
just as additional versions to Angelo's suggestions: BTW: (args is not a list but a tuple!) ;-)
âȘïž(1) using built-in function min()
âȘïž(2) using sorted() built-in function together with index
happy coding!
+ 2
Lothar you are totally right, I've made a mistake, args is indeed a tuple, thanks for the correction :D
+ 1
It would be clearer using
def my_min(*args)
args is just a list so you have to find the min of that list (you could use the min function, but it may be considered cheating :) )
You can find the min of a list either iterativly or recursively, the choice is yours
Errata Corrige: as Lothar pointed out, args is not a list, it's a tuple
+ 1
def my_min(x, *args):
y = min(arg)
if x < y :
return x
else:
return y
print(*args)
print(my_min(8, 13, 4, 42, 120, 7))
0
I was stuck too but reading the solutions i came up ith this:
def my_min(x, *args):
y = min(args)
if y < x:
return y
else:
return x
0
Hi ! I came up with this:
def my_min(x, y, *args):
if x < y and x< min(args):
return x
elif y<x and y<min(args):
return y
else:
return min(args)
print(my_min(8, 13, 4, 42, 120, 7))