lib/easystats.rb in easystats-0.0.3 vs lib/easystats.rb in easystats-0.0.4
- old
+ new
@@ -1,139 +1,145 @@
-class Easystats
- def self
+ class Object
+ # take in an array of numbers and calculate the sum
+ def sum
+ data = self
+ sum_of_numbers = 0
- end
+ data.each do |num|
+ sum_of_numbers += num
+ end
- # take in an array of numbers and calculate the sum
- def sum(data)
- sum_of_numbers = 0
+ sum_of_numbers
+ end
- data.each do |num|
- sum_of_numbers += num
+ # take in an array of numbers and calculate the mean (average)
+ def mean
+ data = self
+ self.sum / data.count.to_f
end
- sum_of_numbers
- end
+ # this is an internat function (technically the developer can use it but should have no need)
+ # this function returns the sum of each squared difference of mean
+ def sum_of_deviations_squared
+ data = self
- # take in an array of numbers and calculate the mean (average)
- def mean(data)
- self.sum(data) / data.count.to_f
- end
+ deviations = Array.new
+ average = self.mean
+ sum_of_deviations = 0
- # this is an internat function (technically the developer can use it but should have no need)
- # this function returns the sum of each squared difference of mean
- def sum_of_deviations_squared(data)
- deviations = Array.new
- average = self.mean(data)
- sum_of_deviations = 0
+ data.each do |num|
+ deviations.push((num-average)**2)
+ end
- data.each do |num|
- deviations.push((num-average)**2)
+ deviations.each do |num|
+ sum_of_deviations += num
+ end
+
+ sum_of_deviations
end
- deviations.each do |num|
- sum_of_deviations += num
+ # take in an array of numbers and calculate the standard deviation
+ def standard_deviation
+ data = self
+ sum_of_deviations = self.sum_of_deviations_squared
+
+ Math::sqrt(sum_of_deviations / (data.count-1))
end
- sum_of_deviations
- end
+ def variance
+ data = self
+ average = self.mean
+ sum_of_deviations = self.sum_of_deviations_squared
+ sum_of_deviations / data.count.to_f
+ end
- # take in an array of numbers and calculate the standard deviation
- def standard_deviation(data)
- sum_of_deviations = self.sum_of_deviations_squared(data)
+ # take in the array of numbers and calculate the median
+ def median
+ data = self
- Math::sqrt(sum_of_deviations / (data.count-1))
- end
+ halfway = data.count / 2
- def variance(data)
- average = self.mean(data)
- sum_of_deviations = self.sum_of_deviations_squared(data)
- sum_of_deviations / data.count.to_f
- end
+ # Sort the array
+ data = data.sort
- # take in the array of numbers and calculate the median
- def median(data)
- halfway = data.count / 2
+ # 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
- # Sort the array
- data = data.sort
+ # Else, there is an odd number of elements in the array
+ else
+ median = data[halfway]
+ end
- # 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
+ median
+ end
- # Else, there is an odd number of elements in the array
- else
- median = data[halfway]
+ # take in an array of numbers and calculate the range
+ def range
+ data = self
+ data = data.sort
+ data[data.count-1] - data[0]
end
- median
- end
+ # take in an array of numbers and return the mode
+ def mode
+ data = self
- # take in an array of numbers and calculate the range
- def range(data)
- # Sort the array
- data = data.sort
- data[data.count-1] - data[0]
- end
+ # Sort the array
+ data = data.sort
- # take in an array of numbers and return the mode
- def mode(data)
- # Sort the array
- data = data.sort
+ # create a flag to tell the user if all the numbers only appear once
+ no_mode = true
- # create a flag to tell the user if all the numbers only appear once
- no_mode = true
-
- # The variable that will hold the highest number
- highest_value = 0
+ # The variable that will hold the highest number
+ highest_value = 0
- # The variable that holds the most time the value appears
- most_times = 0
+ # The variable that holds the most time the value appears
+ most_times = 0
- # Create a new hash to hold the numbers
- tmp = Hash.new
+ # Create a new hash to hold the numbers
+ tmp = Hash.new
- # Populate the hash
- data.each do |num|
- if tmp["#{num}"].nil? == false
- tmp["#{num}"] = tmp["#{num}"].to_i + 1
- else
- tmp["#{num}"] = 1
- end
- end
-
- # Check to make sure that there is a mode
- data.each do |num|
- if tmp["#{num}"].to_i > 1
- no_mode = false
- end
- end
-
- if no_mode == true
- "There is no mode"
- else
+ # Populate the hash
data.each do |num|
- if tmp["#{num}"].to_i > most_times
- highest_value = num
- most_times = tmp["#{num}"]
- end
+ if tmp["#{num}"].nil? == false
+ tmp["#{num}"] = tmp["#{num}"].to_i + 1
+ else
+ tmp["#{num}"] = 1
+ end
end
- # now loop through again just to make sure another number doesn't appear an equal number of times
+ # Check to make sure that there is a mode
data.each do |num|
- if num != highest_value
- if tmp["#{num}"].to_i == most_times
- no_mode = true
- end
+ if tmp["#{num}"].to_i > 1
+ no_mode = false
end
end
if no_mode == true
"There is no mode"
else
- highest_value
+ data.each do |num|
+ if tmp["#{num}"].to_i > most_times
+ highest_value = num
+ most_times = tmp["#{num}"]
+ end
+ end
+
+ # now loop through again just to make sure another number doesn't appear an equal number of times
+ data.each do |num|
+ if num != highest_value
+ if tmp["#{num}"].to_i == most_times
+ no_mode = true
+ end
+ end
+ end
+
+ if no_mode == true
+ "There is no mode"
+ else
+ highest_value
+ end
end
end
end
-end