Skip to content

Instantly share code, notes, and snippets.

@mattdvhope
Last active December 20, 2015 00:39
Show Gist options
  • Save mattdvhope/6043860 to your computer and use it in GitHub Desktop.
Save mattdvhope/6043860 to your computer and use it in GitHub Desktop.
Socrates - Calculating the median of an array of numbers
Median of an array of numbers
def median(array)
if array.length.even?
return (array[((array.length / 2) - 1)].to_f + array[array.length / 2].to_f) / 2
else
return array[array.length / 2]
end
end
array1 = [1, 2, 3, 4, 5, 5, 7]
puts array1.inspect
puts "Median is " + median(array1).to_s
array2 = [4, 4, 5, 5, 6, 6, 6, 7]
puts array2.inspect
puts "Median is " + median(array2).to_s
@mattdvhope
Copy link
Author

Thanks so much for getting me back on track--out of the weeds!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment