Last active
August 29, 2015 14:11
-
-
Save adi89/c69bad56bd18a3605b8f to your computer and use it in GitHub Desktop.
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
| #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 ) } |
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 '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