Created
July 22, 2015 13:56
-
-
Save ejmurray/21fa6019457d10e5d757 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/python | |
# encoding: utf-8 | |
""" | |
Created: 22/07/15, 14:02 | |
Description: return the median value from a sorted list of numbers, | |
an odd length list will have the median value at the centre | |
an list with an even set of numbers will have the median at n and n-1 | |
In the online exercise you need to use 2.0 in the division to get the floating point number. | |
""" | |
def median(x): | |
new_list = sorted(x) | |
size = len(new_list) | |
print(size) | |
if size % 2 == 0: | |
middle_value = int(size / 2) | |
print(middle_value) | |
middle_less_one = int(middle_value - 1) | |
print(middle_less_one) | |
median_value = (new_list[middle_less_one] + new_list[middle_value])/2.0 | |
print(median) | |
else: | |
middle_value = int(size / 2) | |
median_value = new_list[middle_value] | |
return new_list, median_value | |
a = median([4 , 4, 5, 5]) | |
print(a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment