-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmedian.py
More file actions
23 lines (19 loc) · 837 Bytes
/
median.py
File metadata and controls
23 lines (19 loc) · 837 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#Find the median number from a given sequence.
#The sequence could be in any order and hence it is sorted first
#If the sequence contains an even number of elements, the median
#will be the average of the middle two elements.
def median(x):
x = sorted(x) #Sort the list into ascending order
num = x[len(x)/2] #This gets us the first of our middle numbers.
num2 = x[len(x)/2 - 1] #We subtract one to get the second middle number.
if len(x) % 2 == 0:
return (float(num) + num2)/2 #Use float so that the result will be in floating point
else:
return x[len(x)/2]
print median([1,2,3,4])
print median([1,2,3,4,5])
# Key learning:
# In the case of decimal point result
# Python will divide and give us a round number
# and store the remainder in memory unless
# the numerator is a floating point number