Find the minimum of a list without using the min function
PierrotLeFou -
Hello,
I need to solve the following exercise:
Write a function called minimum that accepts a list of numbers as an argument and returns its minimum value. In the case of an empty list, return the value None.
Note well that for the purpose of this exercise, you MUST NOT use the standard min function, and it has actually been temporarily made inaccessible in the context of this exercise. Also, note that your function must not print anything.
Here is my code
def minimum(liste): max = liste[0] longueur = len(liste) indice_max = 0 for i in range(longueur): if liste[i] <= minimum: minimum = liste[i] indice_max = i return None
6 réponses
Hello,
You're almost there. The only issue is that right now you're returning None no matter what.
- l2: This line will crash if the list is empty because list[0] will not be defined. You need to first check that the list has at least one element (for example, if len(list) > 0: ..., or more concisely, with the test if list: ...).
- You should rename your variable max to min_value.
- I advise against naming variables min or max because those are the names of two Python primitives, and even though the language allows it, it's generally a bad idea (because by doing so, the Python function is no longer visible, the symbol min or max now refers to your variable).
- If the list is empty, you can directly return None.
- l4: If the list is empty, i.e., if you don't enter the for loop, min_index should be None. So you need to revisit how you initialize it. Since on line 2 you'll have to check if the list is empty, you can directly return None in that check.
- l2-l6-l7: You're comparing list[i] to minimum. However, minimum is the name of your function, so you're comparing two incomparable things. I suspect that based on how you started your code, you would rather want to compare with min_value.
- l3-l5: You can delete line 3 if you replace line 5 with for i in range(len(list)):. The code will remain just as readable and it won't affect its performance.
- l9: If the list is not empty, your function should return min_value.
Can you propose corrected code that takes these recommendations into account?
Good luck
What is value_min used for?
And when do you check if the list is empty?
mininum is the name of the function and you decide that it's a variable...
Hello,
What are the variables indice_max and liste_min for?
And shouldn't valeur_min be liste[0]?
valeur_min should be defined as a large number (99999 for example)
then loop through the elements of the list:
if liste[i] < valeur_min: valeur_min = liste[i]
and after exiting the loop: return(valeur_min)
This can be done in 5 lines of code
Why not give the first one in the list as the minimum and iterate from the second instead of giving a large number as the initial minimum?
I assume we've already tested if the list is not empty.
Your code raised an unexpected exception
Your score is
50/100.Did you read the answer given on Dec. 1, 2022, at 2:27 PM?
I only see the one at 14:42
Read this one as well.