Find the minimum of a list without using the min function

Leblanc41 -  
 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

mamiemando Posted messages 33537 Registration date   Status Modérateur Last intervention   7 927
 

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

0
leblanc41
 

Your code raised an unexpected exception

  • notably in the context of:
    minimum([])
  • the interpreter's message is:
      Traceback (most recent call last): File "solution", line 2, in minimum IndexError: list index out of range 

Your score is 50/100.

def minimum(liste):     valeur_min = liste[0]     indice_max = 0     for i in range(len(liste)):         if liste[i] <= minimum:             minimum = liste[i]             liste_min = i     return valeur_min


  

0
yg_be Posted messages 23437 Registration date   Status Contributeur Last intervention   1 587 > leblanc41
 

Did you read the answer given on Dec. 1, 2022, at 2:27 PM?

0
Leblanc41 > yg_be Posted messages 23437 Registration date   Status Contributeur Last intervention  
 

I only see the one at 14:42

0
yg_be Posted messages 23437 Registration date   Status Contributeur Last intervention   1 587 > Leblanc41
 

Read this one as well.

0
PierrotLeFou
 

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...

0
Phil_1857 Posted messages 1883 Registration date   Status Membre Last intervention   169
 

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

0
yg_be Posted messages 23437 Registration date   Status Contributeur Last intervention   1 587
 

Using one of the elements from the list as a minimum starting point is perfectly valid. Those who prefer a large number can use float('inf').

0
Phil_1857 Posted messages 1883 Registration date   Status Membre Last intervention   169 > yg_be Posted messages 23437 Registration date   Status Contributeur Last intervention  
 

yes, after all ..

min_value = list[0]

for each element in list:

     if element < min_value: min_value = element

return min_value

0
mamiemando Posted messages 33537 Registration date   Status Modérateur Last intervention   7 927 > Phil_1857 Posted messages 1883 Registration date   Status Membre Last intervention  
 

Provided that the list is not empty!

0
Phil_1857 Posted messages 1883 Registration date   Status Membre Last intervention   169 > mamiemando Posted messages 33537 Registration date   Status Modérateur Last intervention  
 

of course ...

I was just showing Leblanc41 that the mechanics are very simple

by reading his code from yesterday at 6:18 PM, we can see that it is not clear for him

0
PierrotLeFou
 

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.

0
Phil_1857 Posted messages 1883 Registration date   Status Membre Last intervention   169
 

yes, that's what I said in my 4:07 PM response

min_value = list[0]

I'm not going through from list[1], but it doesn’t change much ...

0
PierrotLeFou
 

No problem.

0