Skip to content

Instantly share code, notes, and snippets.

@adi89
Last active August 29, 2015 14:11
Show Gist options
  • Select an option

  • Save adi89/c69bad56bd18a3605b8f to your computer and use it in GitHub Desktop.

Select an option

Save adi89/c69bad56bd18a3605b8f to your computer and use it in GitHub Desktop.
#avg highest and lowest. take the highest and lowest out of array.
require 'pry-debugger'
require 'benchmark'
def special_avg(array)
array.sort!
array = remove_outliers(array)
val = array.reduce(:+) / (array.length)
return val
end
def remove_outliers(array)
max = array.max
min = array.min
array.reject!{|i| i == max || i == min }
end
puts special_avg( ( (1..1000000).to_a + (1..1000000).to_a ) )
puts Benchmark.measure { ( (1..1000000).to_a + (1..1000000).to_a ) }
require 'rspec'
require 'pry-debugger'
require_relative 'avg'
describe "avg outlier function" do
it "should calculate the avg" do
avg_val = special_avg([3, 9, 15, 3, 9, 6, 6, 3, 15, 5])
expect(avg_val).to eq 7
end
end
# sample. other test cases, are for POSSIBLE invalid inputs (non integers) and for arrays with recurring values. empty array. large arrays.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment