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!

Recommended Answers

All 4 Replies

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.

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

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

Thanks for your help guys!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.