Search code examples
pythonfunctionmaxminminimum

Finding the Minimum and Maximum of a list of values without sorting or using any built in Functions or Libraries


I need to find the minimum value of a list of values but I am prohibited from using any built in function or libraries. I am allowed to use if else or elif but that is the extent.

I have to use basic operators and (I suppose?) my own intuition to figure this out.

Unfortunately I am not that intuitive and no matter what combination of if else statements I use or < > operators, I just cannot get it to work.

I'm really new to python and my teacher did little to help describe how to even go about doing this (other than defining operators. That's literally all he did).

So basically I have a list of three integers and I have to find the min and max of those three integers (gotta find some other stuff too but let's keep this simple). I have to use functions to define the integers and separate functions to define the min and max of the list.

I have the function for defining the integers, that was easy, now I'm stuck on this:

def determine_min_value(a,b,c):
    print ("The minimum value is {}.".format(c < (b < a)))

with a, b, and c being variables (num1, num2, num3)

it outputs "The minimum value is False." which would be great if False was a really small number, and also one of the input variables; which it obviously is not.

I'm fully aware that .format(c < (b < a)) is not going to give me what I want, but that was my latest last ditch effort. Like I said I was trying if else statements, but I couldn't figure those out either.

I simply have no idea where to go from here and any input would be greatly appreciated. Also I know you want to say "Just put them in a list and use min(1)" but again, I am not allowed. I would love nothing more than to do that as well.


Solution

  • EDIT: Since you cannot use reduce(), you can do it inline for three integers as follows:

    maximum = lambda x, y, z: x if x > (y and z) else y if y > z else z
    maximum(1, 5, 7)
    

    If you really want to avoid lambda for the sake of a professor then you could write it traditionally:

    def maximum (x, y, z):
        if x > y and z:
            return x
        elif y > z:
            return y
        else:
            return z
    

    To expand the above functionality when the input scales very large would best be implemented the use of a built in function called reduce (especially if you are trying to do something other than check max or min which is obviously a built-in functionality).

    from functools import reduce
    
    def check_greater(a,b):
        if a > b:
            return b
        else:
            return a
    
    number_list = [1,6,3,8,5,78,4,23,9]
    
    minimum = reduce(check_greater, number_list)
    

    This will apply the check_greater() function to every pair of elements in the list and will continually return one value for every pair, corresponding to which is lower.

    First 1 and 6 will be compared. Since 1 is lower, it will be returned in place of both of those values. Now 1 and 3 will be compared. Again, since it is lower it will be returned to replace both of those values. Perhaps I should have nested 1 somewhere in the middle to make it more obvious but no matter where the minimum value falls in the list of numbers, the reduce function will take your entire iterable and return a single value based upon the function you pass as the first argument.

    Note that you can do the same with a maximum, just switch a and b in the return statements of the check_greater() function.