Created
December 31, 2012 23:55
-
-
Save kmandreza/4423999 to your computer and use it in GitHub Desktop.
Write a method mean which takes an Array of numbers as its input and returns the average (mean) value as a Float. For example, mean([1,2,3]) # => 2.0
mean([4.5, 0, -1]) # => 1.1666666666666667
mean([-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 mean(array) | |
| (array.inject { |sum,x| sum + x }).to_f / array.length | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment