Given 3 numbers, I need to find which number lies between the two others. Show 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? asked Apr 2, 2012 at 15:55
2 Put them in a list, sort them, pick the middle one. answered Apr 2, 2012 at 15:56
Sven MarnachSven Marnach 543k114 gold badges914 silver badges816 bronze badges 5
answered Apr 2, 2012 at 16:01
jamylakjamylak 123k29 gold badges227 silver badges227 bronze badges The fastest obvious way for three numbers
answered Apr 2, 2012 at 16:45
Zaur NasibovZaur Nasibov 21.8k11 gold badges51 silver badges80 bronze badges 1 What you want is the median. You can use this code below for any number of numbers:
for a custom solution you can visit this page. answered Apr 2, 2012 at 16:08
Thanasis PetsasThanasis Petsas 4,2785 gold badges30 silver badges56 bronze badges 2 You could do
answered Apr 2, 2012 at 15:57
David RobinsonDavid Robinson 75.5k15 gold badges162 silver badges180 bronze badges 1 This is a O(n) implementation of the median using cumulative distributions. It's faster than sorting, because sorting is O(ln(n)*n).
answered Apr 2, 2012 at 16:15
luke14freeluke14free 2,5011 gold badge16 silver badges25 bronze badges 2 Check this (Suppose list already sorted):
answered Oct 29, 2013 at 3:29
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.
answered Apr 25, 2020 at 7:38
samvsamv 112 silver badges3 bronze badges Here is my attempt using a more pythonic version
answered Jan 12, 2017 at 7:28
MichaelMichael 2,2721 gold badge32 silver badges53 bronze badges If you wish to avoid sorting, you can do:
answered Mar 30, 2017 at 19:11
Cabbage soupCabbage soup 1,2321 gold badge18 silver badges26 bronze badges How do you find the middle number in Python?“how to find middle value of a python list” Code Answer's. def findMiddle(input_list):. middle = float(len(input_list))/2.. if middle % 2 != 0:. return input_list[int(middle - .5)]. return (input_list[int(middle)], input_list[int(middle-1)]). How do you find the median of 3 numbers in Python?Write a Python program to find the median of three values. if num1 < num3: median = num1.. elif b > num3: median = num2.. else: median = num3.. if num1 > num3: median = num1.. elif num2 < num3: median = num2.. else: median = num3.. How do you find the median of three numbers?To find the median of any set of numbers, put them in order from smallest to greatest. If a number occurs more than once, list it more than once. The number in the middle is the median. If there is an even number of numbers, the median is the average of the two numbers in the middle.
|