hao wang
hao wang

Reputation: 45

finding the min value using loop python

I can find max value, I can find average but I just can't seem to find the min. I know there is a way to find max and min in a loop but right now I can only find the max.

def large(s)
    sum=0
    n=0
    for number in s:
        if number>n:
            n=number 
    return n 

Is there a way to find the min value using this function?

Upvotes: 1

Views: 20296

Answers (8)

Kingsley
Kingsley

Reputation: 1

def min(s):

n=s[0]

for number in s:                
    if number < n:        
        n=number                                 
return n                                            

Upvotes: 0

Krim Delko
Krim Delko

Reputation: 11

You can use same function with iteration just instead of n=0 use n=L[0]

def min(L): n=L[0] for x in L: if x

Upvotes: 1

Ars3nous
Ars3nous

Reputation: 136

Using this function to find minimum is

min=-large(-s)

The logic is just to find maximum of the negative list , which gives the minimum value

Upvotes: 1

joaquin
joaquin

Reputation: 85693

You must set n to a very high number (higher than any of the expected) or to take one from the list to start comparison:

def large(s)
    n = s.pop()
    for number in s:
        if number < n:
            n = number 
    return n 

Obviously you have already max and min for this purpose.

Upvotes: 2

Raymond Hettinger
Raymond Hettinger

Reputation: 226764

You can use Python's built-in sum(), min(), and max() functions for this kind of analysis.

However, if you're wanting to do it all in one pass or just want to learn how to write it yourself, then the process is 1) iterate over the input and 2) keep track of the cumulative sum, the minimum value seen so far, and the maximum value seen so far:

def stats(iterable):
    '''Return a tuple of the minimum, average, and maximum values

        >>> stats([20, 50, 30, 40])
        (20, 35.0, 50)

    '''
    it = iter(iterable)
    first = next(it)     # Raises an exception if the input is empty
    minimum = maximum = cumsum = first
    n = 1
    for x in it:
        n += 1
        cumsum += x
        if x < minimum:
            minimum = x
        if x > maximum:
            maximum = x
    average = cumsum / float(n)
    return minimum, average, maximum

if __name__ == '__main__':
    import doctest
    print doctest.testmod()

The code has one other nuance. It uses the first value from the input iterable as the starting value for the minimum, maximum, and cumulative sum. This is preferred over creating a positive or negative infinity value as initial values for the maximum and minimum. FWIW, Python's own builtin functions are written this way.

Upvotes: 3

Carl F.
Carl F.

Reputation: 7056

Why not just replace large with small and > with <? Also, you might not want to initialize n to 0 if you're looking for the smallest value. Your large function only works for lists of positive numbers. Also, you're missing a ":" after your def line.

def small(s):
    if len(s)==0: return None
    n = s[0]
    for number in s[1:]:
        if n < number:
            n=number 
    return n 

This handles empty lists by returning None.

Upvotes: 1

&#211;scar L&#243;pez
&#211;scar L&#243;pez

Reputation: 236170

A straightforward solution:

def minimum(lst):
    n = float('+inf')
    for num in lst:
        if num < n:
            n = num
    return n

Explanation: first, you initialize n (the minimum number) to a very large value, in such a way that any other number will be smaller than it - for example, the infinite value. It's an initialization trick, in case the list is empty, it will return infinite, meaning with that that the list was empty and it didn't contain a minimum value.

After that, we iterate over all the values in the list, checking each one to see if it is smaller than the value we assumed to be the minimum. If a new minimum is found, we update the value of n.

At the end, we return the minimum value found.

Upvotes: 1

Fred Foo
Fred Foo

Reputation: 363858

Finding the minimum takes the same algorithm as finding the maximum, but with the comparison reversed. < becomes > and vice versa. Initialize the minimum to the largest value possible, which is float("inf"), or to the first element of the list.

FYI, Python has a builtin min function for this purpose.

Upvotes: 2

Related Questions