Last active
September 4, 2016 18:20
-
-
Save ooade/21d9e54a4a3c8ff9256efe2913e1dac8 to your computer and use it in GitHub Desktop.
Median
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
def median(nums): | |
nums = sorted(nums) | |
if len(nums) % 2 == 0: | |
# if even, grab the two mid letters | |
return (nums[int(len(nums) / 2) - 1] + nums[int(len(nums) / 2)]) / 2.0 | |
else: | |
return nums[int(len(nums) / 2)] | |
print median([4, 5, 5, 4]) #returns 4.5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment