+3

# *args in python

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. def my_min(x, y): if x < y: return x else: return y print(my_min(8, 13, 4, 42, 120, 7))

3/14/2021 2:49:37 PM

pardeep

+3

def my_min(*args): minval = args[0] for num in args: if num<minval: minval=num return minval print(my_min(8, 13, 4, 42, 120, 7))

+2

faster approach def myfunc(*args): return min(*args) print(myfunc(1,2,3,4)) print(myfunc([1,2,3,4]))

+1

You should add the language name in tags while asking a question.

+1

def myFunc(*args): ... will populate args with a list containing all non keywords args. you should just find min value of this list and return it ;)

+1

AssasinStudent if OP have to implement a my_min() function, he's probably not allowed to use min() built-in... and if he could, shorter would be: my_min = min anyway, if you give complete solution to OP, he will learn nothing appart copy-pasting without understanding ^^

0

which language?

0

python

0

ya thanks

0

Agree. Giving hints to an OP rather than a complete solution is recommended in this section.

0

Look I have following solution it may help you. def my_min(x, *args): if len(args) == 1: if x < args[0]: return x else: return args[0] y, *args = args if x < y: return my_min(x, *args) else: return my_min(y, *args) print(my_min(8, 13, 4, 42, 120, 7)) It's a recursive way of finding minimum value DHANANJAY