Skip to content
Advertisement

How can I find the minimum in Python?

This function finds the maximum and minimum values.

n=int(input())
array=list(map(int,input().split))

for i in range(n):
    max=array[0]
    if i>0:
        if max<array[i]:
            max=array[i]
       
for i in range(n):
    min=array[0]
    if i>0:
        if min>array[i]:
            min=array[i]   
            
print(max,end='')
print(     min)

The maximum value comes out normally, but the minimum value comes out as the first array value. I can’t find what is wrong.

Advertisement

Answer

The maximum value comes out normally

I think it does only because either your first or last value is the max of the sequence. Because you completely reset both min and max on each iteration. So the entire loop is useless, any iteration but the last has no effect.

The initialisation should be outside the loop, not inside of it:

max=array[0]
for i in range(n):
    if i>0:
        if max<array[i]:
            max=array[i]

min=array[0]
for i in range(n):
    if i>0:
        if min>array[i]:
            min=array[i] 

At which point the check on the index is clearly unnecessary: either eat the unnecessary comparison of array[0] to array[0] (it’s not harmful per-se), or just skip the index when iterating:

max=array[0]
for i in range(1, n):
    if max<array[i]:
        max=array[i]

min=array[0]
for i in range(1, n):
    if min>array[i]:
        min=array[i] 

In essence you’ve written a very complicated version of:

if array[0] < array[-1]:
    max = array[-1]
else:
    max = array[0]

if array[0] > array[-1]:
    min = array[-1]
else:
    min = array[0]

Now for further improvements, under the assumption that you’re trying to learn we’ll ignore that min and max are already built-in functions and thus the entire thing is redundant (although you should not name your own variables the same as builtins, as that creates confusion):

  • n is useless, because it’s not checked against array and array has its own length, n can only trigger unnecessary errors if it exceeds len(array), or skip item if it’s smaller than len(array). n might be useful if e.g. the input was gathered in a loop of validated input() call, which is not the case.
  • Good error handling would probably check that the length of the array is at least 1, otherwise the script will blow up (although it would also need to check that individual input values are valid before converting to integers so…)
  • You can extract min and max in the same loop, no need to loop twice (probably not very important in this case).
  • But Python also has good support for iterators, so you should avoid explicit indexing when not necessary, which it is not here.

And my take, still within the realm of trying to learn, would be:

array=list(map(int,input().split))

low = high = array[0]
for candidate in array[1:]:
    if candidate > high:
        high = candidate
    if candidate < low:
        low = candidate

print(f"{low} ... {high}")

An other fun alternative is to sort the array and take the first and last elements of the now-sorted array:

array=list(map(int,input().split))

low, *_, high = sorted(array)

print(f"{low} ... {high}")

although it has the drawback that it only works on array of length 2+ where the original works on “singleton” arrays.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement