Last active
December 22, 2015 13:19
-
-
Save dmehrotra/6478105 to your computer and use it in GitHub Desktop.
stats
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
require 'rspec' | |
require_relative 'array_statistics' | |
describe ArrayStats do | |
it 'Input of an array gives the largest number in the array' do | |
expect(ArrayStats.new([1, 2, 3, 4, 2, 5, 3]).largest_num).to eql(5) | |
end | |
it 'Input of an array gives the smallest number in the array' do | |
expect(ArrayStats.new([1, 2, 3, 4, 2, 5, 3]).smallest_num).to eql(1) | |
end | |
it 'Input of an array gives the average of the array' do | |
expect(ArrayStats.new([1, 2, 3, 4, 2, 5, 3]).average).to eql(2.86) | |
end | |
it 'Input of an array gives the stdev of the array' do | |
expect(ArrayStats.new([1, 2, 3, 4, 2, 5, 3]).stdev).to eql(1.25) | |
end | |
it 'Input of an array gives the nth smallest number in the array' do | |
expect(ArrayStats.new([1, 2, 3, 4, 2, 5, 3]).nth_smallest(3)).to eql(2) | |
end | |
################## | |
it 'Input of an array gives the sum of the array' do | |
expect(ArrayStats.new([1, 2, 3, 4, 2, 5, 3]).sum).to eql(20) | |
end | |
it 'Input of an array gives the product of the array' do | |
expect(ArrayStats.new([1, 2, 3, 4, 2, 5, 3]).product).to eql(720) | |
end | |
it 'Input of an array gives difference between highest and lowest' do | |
expect(ArrayStats.new([1, 2, 3, 4, 2, 5, 3]).difference).to eql(4) | |
end | |
it 'Input of an array gives digit frequency' do | |
expect(ArrayStats.new([1, 1000, 200, 300, 3000, 4000, 5500]).digit_frequency).to eql({ | |
1 => 1, | |
3 => 2, | |
4 => 4 | |
}) | |
end | |
end |
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
class ArrayStats | |
attr_reader :sum | |
def initialize(array) | |
@array = array | |
average | |
end | |
def largest_num | |
@array.sort.last | |
end | |
def smallest_num | |
@array.sort.first | |
end | |
def average | |
@sum = 0.00 | |
@array.each do |number| | |
@sum += number | |
end | |
@average = (@sum/@array.length).round(2) | |
@sum = @sum.to_i | |
@average | |
end | |
def stdev | |
distance_squared = 0.00 | |
@array.each do |number| | |
distance_squared += (number - @average)**2 | |
end | |
Math.sqrt(distance_squared/@array.length).round(2) | |
end | |
def nth_smallest(nth) | |
@array.sort[nth-1] | |
end | |
def product | |
@product = 1 | |
@array.each do |number| | |
@product *= number | |
end | |
@product | |
end | |
def difference | |
@array.sort.last - @array.sort.first | |
end | |
def digit_frequency | |
total_array = [] | |
@array.each do |number| | |
total_array << number.to_s.length | |
end | |
freq = total_array.inject(Hash.new(0)) { |h,v| h[v] += 1; h } | |
p freq | |
end | |
end | |
arraystats = ArrayStats.new([1, 2, 3, 4, 2, 5, 3]) | |
arraystats2 = ArrayStats.new([1, 1000, 200, 300, 3000, 4000, 5500]) | |
arraystats2.digit_frequency |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment