Skip to content

Commit

Permalink
Simplify #median
Browse files Browse the repository at this point in the history
  • Loading branch information
justincampbell committed Apr 17, 2012
1 parent e55573d commit c5793af
Showing 1 changed file with 4 additions and 13 deletions.
17 changes: 4 additions & 13 deletions lib/easystats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,15 @@ def mean
def median
return if self.empty?

data = self
data = self.sort

halfway = data.count / 2

# Sort the array
data = data.sort

# The median will be different based on the number of numbers in the array
# If there is an even number in the array
if(data.count % 2) == 0
median = (data[halfway] + data[halfway-1]) / 2.0

# Else, there is an odd number of elements in the array
if data.count.even?
(data[halfway] + data[halfway - 1]) / 2.0
else
median = data[halfway]
data[halfway]
end

median
end unless method_defined? :median

# take in an array of numbers and return the mode
Expand Down

0 comments on commit c5793af

Please sign in to comment.