Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kmandreza/4429574 to your computer and use it in GitHub Desktop.
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
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