0

Hi there,

I was wondering if anyone could help me find a procedure in python that takes in an unsorted list, and returns the difference between the largest and smallest values in the list, without using the built in max/min/sort procedures. Thank you!

3
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by deedee2
1

Make the min_item the first item in the list. Then iterate through the list and compare. If the list item is less than the present min_item, make it the new min_item.

Do a similar thing with max_item.

0

Warning: Code is not tested:

def difminmax(li):
    minimum = li[0]
    maximum = li[0]
    for value in li:
        if value < minimum:
            minimum = value

        if value > maximum:
            maximum = value

    return maximum - minimum
0

You can help folks with their homework, but do not give a total solution. They usually learn better that way!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.