Created
February 27, 2019 12:38
-
-
Save eLtronicsVilla/733a7e478700679f31128312dcff407c to your computer and use it in GitHub Desktop.
Calculate the median of the given data set
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is sample code,function to calculate the median of given dataset | |
data = [2,3,4,5,6,8,9] | |
def median(value): | |
# find middle most value for even or odd data set | |
n = len(value) # find out the length | |
sorted_value = sorted(value) #sort the value | |
mid_p = n // 2 # find out the mid point | |
if n % 2 == True: | |
# if odd return the middle value | |
return sorted_value[mid_p] | |
else: | |
# if even value then take the average of middle values | |
low = mid_p-1 | |
high = mid_p | |
return (sorted_value[low] + sorted_value[high]) / 2 | |
median(data) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment