0

How to find max and min of a list using a recurring function

By recurring function I mean a function that works like a loop: the one that calls herself at the end... PS: i know the name in frensh as "fonction récursive" but i am not sure regarding its technical name in english 🤔 Please help, if u understood my unclear question of course 😅

2nd Feb 2019, 1:53 PM
mayssa rekik
mayssa rekik - avatar
15 Réponses
+ 3
So here is mine pretty ugly but still working solution: https://code.sololearn.com/cz66oXF1G3bQ/?ref=app Also has error catching Btw. it uses similar principle as your loop code just put to recursion Pls. all comment any error, i am interested in this one
3rd Feb 2019, 1:57 PM
Maneren
Maneren - avatar
+ 3
In python it doesn't require to use recursive function. It had functions max() and min(). These return respectively maximal and manimal value from list passed as argument.
2nd Feb 2019, 2:22 PM
Maneren
Maneren - avatar
+ 2
It is called recursive function And in what language, becase some of them have predefined functions for this
2nd Feb 2019, 2:12 PM
Maneren
Maneren - avatar
+ 2
If you had to use a loop instead of a recursive function, could you solve it yourself?
3rd Feb 2019, 5:40 AM
Jared Bird
Jared Bird - avatar
+ 2
mayssa rekik so do if it is homework show us your attempt to solve it. This is rules here.
3rd Feb 2019, 7:23 AM
Maneren
Maneren - avatar
+ 1
Oh ok thanks I want it in python
2nd Feb 2019, 2:16 PM
mayssa rekik
mayssa rekik - avatar
+ 1
Jared Bird Yes of course i've already done that before L=list(input('write a numeric list') Max =L[0] While i in range(len(L)) : If L[i] >Max : Max=L[i] Print(Max)
3rd Feb 2019, 8:31 AM
mayssa rekik
mayssa rekik - avatar
+ 1
Ok, so one way would be close to this. Could you write a function that removes 1 item from the list, then calls it's self until there is 1 left and this is the max?
3rd Feb 2019, 8:42 AM
Jared Bird
Jared Bird - avatar
+ 1
@maneren Here is my attempt to code it using a recursive function Def max (L) : If L==0: return 0 return max(L.del(L[0]))
3rd Feb 2019, 10:42 AM
mayssa rekik
mayssa rekik - avatar
+ 1
Maneren you ready to give the answer away already? BTW it does not work for a list of negative integers.
3rd Feb 2019, 11:19 PM
Jared Bird
Jared Bird - avatar
+ 1
Jared Bird oh i forgot it is Python and not Javascript. And what is the problem with answering?
4th Feb 2019, 6:02 AM
Maneren
Maneren - avatar
+ 1
Maneren I hope you are not taking it personally. It's just when it's for some else's home work I prefer to give hints and not write the code for them.
4th Feb 2019, 10:48 PM
Jared Bird
Jared Bird - avatar
+ 1
Jared Bird ok, get it and no problem i am just little bit more straightforward PS. i gave just the idea, as you mentioned it doesn't work for negative, and also is just for max not min So asker has to work on it little bit
5th Feb 2019, 6:12 AM
Maneren
Maneren - avatar
+ 1
Jared Bird Maneren Thanks a lot guys you've been really helpful 😁😄
5th Feb 2019, 9:51 AM
mayssa rekik
mayssa rekik - avatar
0
Yes i do know that but i am asked to code a function that does the same thing of a pre-defined function PS it's a homework 😐
2nd Feb 2019, 2:39 PM
mayssa rekik
mayssa rekik - avatar