15. Median alternative


#1

Hey, I was doing this one and I thought maybe there was a method with the [ max() and min()] functions. Just checking if there is
`


#2

You can't find median when you only know the minimum and maximum.

To easily prove that, take a look at these two lists:

x = [1, 61, 100]
y = [1, 2, 100]

In both cases, the minimum and maximum are exactly the same. Medians are completely different.


If I misunderstood your question, please rephrase it.


#3

Here's a solution using max and min. It is offered as a demo, and definitely not as a recommended solution ...

def median(nums):
    # Try this using the max and min functions
    # This is a demo; not recommended as a solution
    if len(nums) > 2:
        return median(sorted(nums)[1:-1])
    return (max(nums) + min(nums)) / 2.0

#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.