average 2.0 → 3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/average/array_overrides.rb +6 -6
- data/lib/average/mean.rb +1 -1
- data/lib/average/median.rb +2 -2
- data/lib/average/mode.rb +1 -1
- data/lib/average/version.rb +1 -1
- data/spec/array_overrides_spec.rb +12 -12
- data/spec/mean_spec.rb +4 -4
- data/spec/median_spec.rb +4 -4
- data/spec/mode_spec.rb +4 -4
- metadata +1 -1
@@ -1,15 +1,15 @@
|
|
1
1
|
require 'average/mean'
|
2
2
|
require 'average/median'
|
3
3
|
class Array
|
4
|
-
def
|
5
|
-
|
4
|
+
def mean
|
5
|
+
get_mean(self)
|
6
6
|
end
|
7
7
|
|
8
|
-
def
|
9
|
-
|
8
|
+
def mode
|
9
|
+
get_mode(self)
|
10
10
|
end
|
11
11
|
|
12
|
-
def
|
13
|
-
|
12
|
+
def median
|
13
|
+
get_median(self)
|
14
14
|
end
|
15
15
|
end
|
data/lib/average/mean.rb
CHANGED
data/lib/average/median.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
def
|
1
|
+
def get_median(array)
|
2
2
|
return if not valid_array?(array)
|
3
3
|
array = clean_array(array)
|
4
4
|
calculate_median(array)
|
@@ -7,7 +7,7 @@ end
|
|
7
7
|
def calculate_median(array)
|
8
8
|
array.sort!
|
9
9
|
if array.length.even?
|
10
|
-
|
10
|
+
get_mean( middle_items_to_average(array) )
|
11
11
|
else
|
12
12
|
array[ array.length / 2.to_f ]
|
13
13
|
end
|
data/lib/average/mode.rb
CHANGED
data/lib/average/version.rb
CHANGED
@@ -4,28 +4,28 @@ describe Average do
|
|
4
4
|
|
5
5
|
let(:int_array) { [3,4,5] }
|
6
6
|
|
7
|
-
describe '.
|
7
|
+
describe '.mean' do
|
8
8
|
context 'should give back the same result as' do
|
9
|
-
it '
|
10
|
-
expect(int_array.
|
9
|
+
it 'get_mean([X,X,X,X])' do
|
10
|
+
expect(int_array.mean).to eq(get_mean(int_array))
|
11
11
|
end
|
12
|
-
|
12
|
+
end
|
13
13
|
end
|
14
14
|
|
15
|
-
describe '.
|
15
|
+
describe '.mode' do
|
16
16
|
context 'should give back the same result as ' do
|
17
|
-
it '
|
18
|
-
|
17
|
+
it 'get_mode([X,X,X,X])' do
|
18
|
+
expect(int_array.mode).to eq(get_mode(int_array))
|
19
19
|
end
|
20
|
-
|
20
|
+
end
|
21
21
|
end
|
22
22
|
|
23
|
-
describe '.
|
23
|
+
describe '.median' do
|
24
24
|
context 'should give back the same result as ' do
|
25
|
-
it '
|
26
|
-
expect(int_array.
|
25
|
+
it 'get_median([X,X,X,X])' do
|
26
|
+
expect(int_array.median).to eq(get_median(int_array))
|
27
27
|
end
|
28
|
-
|
28
|
+
end
|
29
29
|
end
|
30
30
|
|
31
31
|
end
|
data/spec/mean_spec.rb
CHANGED
@@ -6,23 +6,23 @@ describe Average do
|
|
6
6
|
|
7
7
|
let(:mixed_array) { [3, 4.0, 5, 2.0, 3, 1.0] }
|
8
8
|
|
9
|
-
describe '#
|
9
|
+
describe '#get_mean' do
|
10
10
|
|
11
11
|
context 'with a array of integers' do
|
12
12
|
it 'should calculate the correct mean' do
|
13
|
-
expect(
|
13
|
+
expect(get_mean(int_array)).to eq(4)
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
17
|
context 'with a mixed array of integers and floats' do
|
18
18
|
it 'should calculate the correct mean' do
|
19
|
-
expect(
|
19
|
+
expect(get_mean(mixed_array)).to eq(3)
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
23
|
context 'with a empty array' do
|
24
24
|
it 'should give back a nil' do
|
25
|
-
expect(
|
25
|
+
expect(get_mean([])).to eq(nil)
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
data/spec/median_spec.rb
CHANGED
@@ -12,23 +12,23 @@ describe Average do
|
|
12
12
|
|
13
13
|
let(:mode_array_not_pair) { [1, 1, 2, 2, 3, 4, 5] }
|
14
14
|
|
15
|
-
describe '#
|
15
|
+
describe '#get_median' do
|
16
16
|
|
17
17
|
context 'with a array of integers' do
|
18
18
|
it 'should calculate the correct median' do
|
19
|
-
expect(
|
19
|
+
expect(get_median(int_array)).to eq(4)
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
23
|
context 'with a mixed array of integers and floats' do
|
24
24
|
it 'should calculate the correct median' do
|
25
|
-
expect(
|
25
|
+
expect(get_median(mixed_array)).to eq(3)
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
29
|
context 'with an empty array' do
|
30
30
|
it 'should gives back a nil' do
|
31
|
-
expect(
|
31
|
+
expect(get_median([])).to eq(nil)
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
data/spec/mode_spec.rb
CHANGED
@@ -8,23 +8,23 @@ describe Average do
|
|
8
8
|
|
9
9
|
let(:mixed_array) { [3, 4.0, 5, 2.0, 3, 1.0] }
|
10
10
|
|
11
|
-
describe '#
|
11
|
+
describe '#get_mode' do
|
12
12
|
|
13
13
|
context 'with a array of integers' do
|
14
14
|
it 'should calculate the correct mode' do
|
15
|
-
expect(
|
15
|
+
expect(get_mode(mode_array)).to eq([1, 2])
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
19
|
context 'with a empty array' do
|
20
20
|
it 'should return a empty array' do
|
21
|
-
expect(
|
21
|
+
expect(get_mode([])).to eq(nil)
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
25
|
context 'with a nil object' do
|
26
26
|
it 'should return a empty array' do
|
27
|
-
expect(
|
27
|
+
expect(get_mode([])).to eq(nil)
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|