8

Given 3 numbers, I need to find which number lies between the two others.

ie,given 3,5,2 I need 3 to be returned.

I tried to implement this by going thru all three and using if else conditions to check if each is between the other two.But this seems a naive way to do this.Is there a better way?

2
  • 3
    Which result do you expect for [1,1,2]? Commented Apr 2, 2012 at 15:58
  • for [1,1,2] , 1 should be taken as middle number Commented Apr 2, 2012 at 16:08

10 Answers 10

23

Put them in a list, sort them, pick the middle one.

Sign up to request clarification or add additional context in comments.

5 Comments

Sven, beside being a generic answer, it is bad for the case of 3 numbers. If this kind of subroutine appears in heavy-calculations-related task, creating a list of three elements and sorting it is not the right thing even for Python. Imagine the guy next time working i.e. in c++ and creating a vector of three numbers and sorting it...
@BasicWolf: Have you benchmarked it? The difference is not that large
@Niklas B. The difference is small indeed. My concerns about the overheads of the sorted list solution and it just sounds wrong. Have you seen this: pyvideo.org/video/880/stop-writing-classes ?
@BasicWolf: No, but I'll check it out. I guess it's about how simplicity and reability usually are the most important aspects of your code.
@BasicWolf: In most cases, readability matters more than performance, and you can't beat sorted((a, b, c))[1] in that regard.
16
>>> x = [1,3,2] >>> sorted(x)[len(x) // 2] 2 

Comments

10

The fastest obvious way for three numbers

def mean3(a, b, c): if a <= b <= c or c <= b <= a: return b elif b <= a <= c or c <= a <= b: return a else: return c 

1 Comment

mean3(3,5,2) will give 2 instead of 3 ?
5

What you want is the median. You can use this code below for any number of numbers:

import numpy numbers = [3,5,2] median = numpy.median(numbers) 

for a custom solution you can visit this page.

2 Comments

In the case of the OP I don't think he is ready to be using something like numpy.
Not quite. The OP wants the middle number in a list. Of course, this is somewhat ambiguous for a list with even number of elements. numpy.median([3,5,2]) = 3.0 (float!). numpy.median([3,5,2,1]) = 2.5, not at all what the OP has in mind!
4

You could do

numbers = [3, 5, 2] sorted(numbers)[1] 

1 Comment

Even though this would be completely fine, id just hate to have a hard coded 1
2

This is a O(n) implementation of the median using cumulative distributions. It's faster than sorting, because sorting is O(ln(n)*n).

def median(data): frequency_distribution = {i:0 for i in data} for x in data: frequency_distribution[x] =+ 1 cumulative_sum = 0 for i in data: cumulative_sum += frequency_distribution[i] if (cumulative_sum > int(len(data)*0.5)): return i 

2 Comments

smart solution to a different question though, because here we only have 3 items...
median([8, 1, 4]) gives me 1 !!
1

Check this (Suppose list already sorted):

def median(list): ceil_half_len = math.ceil((len(list)-1)/2) # get the ceil middle element's index floor_half_len = math.floor((len(list)-1)/2) # get the floor middle element 's index' return (list[ceil_half_len] + list[floor_half_len]) / 2 

1 Comment

This returns TypeError: list indices must be integers, not float. You need to wrap math.ceil and math.floor with int()
1

If it's just for 3 numbers. First find the max and the min numbers in list. Remove them, the remainder of list is the medium. If you don't want to use the "if-else" or "sorted()" that is.

def mid(a,b,c): num = [a,b,c] small, big = min(num), max(num) num.remove(small) num.remove(big) return num[0] # return the remainder 

Comments

0

Here is my attempt using a more pythonic version

def median(a): sorted_a = sorted(a) if len(a) % 2 == 0: median = sum(sorted_a[(len(a)//2)-1:(len(a)//2)+1])/2. else: median = sorted_a[(len(a)-1)//2] >>> x = [64630, 11735, 14216, 99233, 14470, 4978, 73429, 38120, 51135, 67060] >>> median(x) >>> 44627.5 >>> y = [1, 2, 3] >>> median(y) >>> 2 

Comments

0

If you wish to avoid sorting, you can do:

def find_median(x): return sum(x) - max(x) - min(x) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.