+ 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)

8th Jul 2021, 5:31 PM
Zach Z
9 Réponses
+ 4
This can be solved using the above mentioned defined function ie like here: https://code.sololearn.com/c098X9UiGgQ1/?ref=app
8th Jul 2021, 8:23 PM
JaScript
JaScript - avatar
+ 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!
8th Jul 2021, 8:30 PM
Lothar
Lothar - avatar
+ 2
Lothar you are totally right, I've made a mistake, args is indeed a tuple, thanks for the correction :D
8th Jul 2021, 10:04 PM
Angelo
Angelo - avatar
+ 2
9th Jul 2021, 6:47 PM
Zach Z
+ 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
8th Jul 2021, 6:19 PM
Angelo
Angelo - avatar
+ 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))
8th Sep 2021, 9:12 PM
Khomi TAKAYANAGI
Khomi TAKAYANAGI - avatar
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
17th Aug 2021, 1:07 PM
Xheni Lamaj
Xheni Lamaj - avatar
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))
26th May 2022, 7:18 PM
Samane Dashtakian