Created
September 6, 2013 18:19
-
-
Save ashleytbasinger/bb92ff4b579079221a07 to your computer and use it in GitHub Desktop.
TDD array assignment code partner project with Kyle
This file contains 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
#require 'Math' | |
class ArrayStatistic | |
def initialize(array) | |
@array = array | |
end | |
def largest | |
@array.sort[-1] | |
end | |
def smallest | |
@array.sort[0] | |
end | |
def average | |
@array.inject(:+) / @array.size | |
end | |
def standard_deviation | |
squared_differences = 0 | |
@array.each do |number| | |
squared_differences += (number - average) ** 2 | |
end | |
variance = squared_differences.to_f / @array.length | |
Math.sqrt(variance) | |
end | |
def nth_smallest(number) | |
@array.sort[number-1] | |
end | |
def sum | |
@array.inject(:+) | |
end | |
def product | |
@array.inject(:*) | |
end | |
def difference | |
largest - smallest | |
end | |
def digit_frequency | |
hash = {} | |
@array.each do |number| | |
digits = number.to_s.length.to_s | |
if hash[digits].nil? | |
hash[digits] = 1 | |
else | |
hash[digits] += 1 | |
end | |
end | |
hash | |
end | |
def frequency | |
hash = {} | |
@array.each do |number| | |
if hash[number.to_s].nil? | |
hash[number.to_s] = 1 | |
else | |
hash[number.to_s] += 1 | |
end | |
end | |
hash | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment