Created
January 1, 2013 19:48
-
-
Save kmandreza/4429574 to your computer and use it in GitHub Desktop.
Write a method median which takes an Array of numbers as its input and returns the median value. You might want to look up the definition of "median."" For example, median([1,2,3]) # => 2
median([4.5, 0, -1]) # => 0
median([-100, 100]) # => 0.0
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(array) #To find the Median, place the numbers you are given in value order and find the middle number. | |
sorted = array.sort | |
if array.length % 2 != 0 | |
array[array.length/2] | |
else | |
((array[array.length/2] - array[(array.length/2)-1]).to_f / 2) + array[(array.length/2)-1] | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment