average 1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in average.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Fernando
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Average
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'average'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install average
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'average/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "average"
8
+ spec.version = Average::VERSION
9
+ spec.authors = ["Fernando"]
10
+ spec.email = ["fgonzalezaguilera@gmail.com"]
11
+ spec.description = %q{Find the arithmethic mean/mode/median of an array of numbers .}
12
+ spec.summary = %q{Find the arithmethic mean/mode/median of an array of numbers .}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency 'rspec'
24
+ end
@@ -0,0 +1,9 @@
1
+ require "average/version"
2
+ require "average/mean"
3
+ require "average/median"
4
+ require "average/mode"
5
+ require "average/average_helper"
6
+
7
+ module Average
8
+
9
+ end
@@ -0,0 +1,43 @@
1
+ def valid_array?(array)
2
+ # Valid if:
3
+ # - the class of the parameter 'array' respond to Array
4
+ # - the parameter 'Array' is not null or empty.
5
+ # - The array given contains Integers, Floats, and Strings that could be turned to a valid digit.
6
+ ( array.is_a?(Array) && !array.nil? && !array.empty? && array_contain_digits?(array) )
7
+ end
8
+
9
+ def array_contain_digits?(array)
10
+ # Go through all elements
11
+ result = true
12
+ array.each do |array_element|
13
+ if not value_can_be_handled?(array_element)
14
+ result = false
15
+ end
16
+ end
17
+ result
18
+ end
19
+
20
+ def value_can_be_handled?(element)
21
+ # Check if the elements of the array 'look like numbers'.
22
+ element.is_a?(Integer) || element.is_a?(Float) || looks_like_a_digit?(element)
23
+ end
24
+
25
+
26
+ def looks_like_a_digit?(digit)
27
+ if digit.is_a?(String) && digit !~ /^\s*[+-]?((\d+_?)*\d+(\.(\d+_?)*\d+)?|\.(\d+_?)*\d+)(\s*|([eE][+-]?(\d+_?)*\d+)\s*)$/
28
+ # is _not_ a string that could be turned to a digit
29
+ false
30
+ else
31
+ # is a string that could be a digit
32
+ true
33
+ end
34
+ end
35
+
36
+ def clean_array(array)
37
+ array.each_with_index do |val, index|
38
+ if val.is_a?(String)
39
+ val.include?('.') ? (array[index] = val.to_f) : (array[index] = val.to_i)
40
+ end
41
+ end
42
+ array
43
+ end
@@ -0,0 +1,5 @@
1
+ def mean(array)
2
+ return if !valid_array?(array)
3
+ array = clean_array(array)
4
+ array.inject(0) { |sum, x| sum += x } / array.size.to_f
5
+ end
@@ -0,0 +1,22 @@
1
+ def median(array)
2
+ return if !valid_array?(array)
3
+ array = clean_array(array)
4
+ get_median(array)
5
+ end
6
+
7
+ def get_median(array)
8
+ array.sort!
9
+ if array.length.even?
10
+ mean(middle_items_to_average(array))
11
+ else
12
+ array[ array.length / 2.to_f ]
13
+ end
14
+ end
15
+
16
+ def middle_item(array)
17
+ array.size / 2
18
+ end
19
+
20
+ def middle_items_to_average(array)
21
+ array[middle_item(array) -1 .. middle_item(array)]
22
+ end
@@ -0,0 +1,20 @@
1
+ def mode(array)
2
+ return if !valid_array?(array)
3
+ array = clean_array(array)
4
+ result = repetition_hash(array)
5
+ result[:hash_result].select { |key, value| value == result[:max_repetition] }.keys.collect { |float| float.to_i }
6
+ end
7
+
8
+ def repetition_hash(array)
9
+ return if !valid_array?(array)
10
+ array = clean_array(array)
11
+ result = array.inject({}) { |key, value| key[value] = array.count(value.to_f); key }
12
+ { hash_result: result, max_repetition: result.values.max }
13
+ end
14
+
15
+ def unique_mode(array)
16
+ # only for Ruby versions bigger than 1.8.7 -> http://apidock.com/ruby/v1_8_7_72/Enumerable/max_by
17
+ return if !valid_array?(array)
18
+ array = clean_array(array)
19
+ array.max_by { |x| array.count(x) }
20
+ end
@@ -0,0 +1,3 @@
1
+ module Average
2
+ VERSION = "1.0"
3
+ end
@@ -0,0 +1,107 @@
1
+ require 'spec_helper'
2
+
3
+ describe Average do
4
+
5
+ let(:valid_array) { [3,'4.0', 5, '2', 0.3] }
6
+
7
+ let(:invalid_array) { [3,'4.0w', 5, 'whatever', 0.3] }
8
+
9
+ describe '#valid_array?' do
10
+ context 'with a mixed array of valid elements' do
11
+ it 'should return true' do
12
+ expect(valid_array?(valid_array)).to be(true)
13
+ end
14
+ end
15
+
16
+ context 'with a mixed array of invalid elements' do
17
+ it 'should return true' do
18
+ expect(valid_array?(invalid_array)).to be(false)
19
+ end
20
+ end
21
+
22
+ context 'with a element that does not have an Array class' do
23
+ it 'should return false' do
24
+ expect(valid_array?('1, 3, 0.1')).to be(false)
25
+ end
26
+ end
27
+
28
+ context 'with a nil value' do
29
+ it 'should return false' do
30
+ expect(valid_array?(nil)).to be(false)
31
+ end
32
+ end
33
+
34
+ context 'with a empty array' do
35
+ it 'should return false' do
36
+ expect(valid_array?([])).to be(false)
37
+ end
38
+ end
39
+
40
+ end
41
+
42
+ describe '#array_contain_digits?' do
43
+ context 'with a mixed array of valid elements' do
44
+ it 'should return true' do
45
+ expect(array_contain_digits?(valid_array)).to be(true)
46
+ end
47
+ end
48
+
49
+ context 'with a mixed array of invalid elements' do
50
+ it 'should return false' do
51
+ expect(array_contain_digits?(invalid_array)).to be(false)
52
+ end
53
+ end
54
+
55
+ end
56
+
57
+ describe '#value_can_be_handled?' do
58
+ context 'when the element is a Integer' do
59
+ it 'should return true' do
60
+ expect(value_can_be_handled?(2)).to be(true)
61
+ end
62
+ end
63
+
64
+ context 'when the element is a Float' do
65
+ it 'should return true' do
66
+ expect(value_can_be_handled?(2.0)).to be(true)
67
+ end
68
+ end
69
+
70
+ context 'when the element is something unknown' do
71
+ it 'should return false' do
72
+ expect(value_can_be_handled?('whatever')).to be(false)
73
+ end
74
+ end
75
+
76
+ end
77
+
78
+ describe '#looks_like_a_digit?' do
79
+ context 'when the element is a string that looks like a Float' do
80
+ it 'should return true' do
81
+ expect(looks_like_a_digit?('9.0')).to be(true)
82
+ end
83
+ end
84
+
85
+ context 'when the element is a string that looks like a Integer' do
86
+ it 'should return true' do
87
+ expect(looks_like_a_digit?('9')).to be(true)
88
+ end
89
+ end
90
+
91
+ context 'when the element is something unknown' do
92
+ it 'should return false' do
93
+ expect(looks_like_a_digit?('whatever')).to be(false)
94
+ end
95
+ end
96
+
97
+ end
98
+
99
+ describe '#clean_array' do
100
+ context 'with an array that contains valid that could be converted to a digit' do
101
+ it 'should return an array without string and with the correct numeric format' do
102
+ expect(clean_array(valid_array)).to eq([3, 4.0, 5, 2, 0.3])
103
+ end
104
+ end
105
+ end
106
+
107
+ end
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+
3
+ describe Average do
4
+
5
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe Average do
4
+
5
+ let(:int_array) { [3,4,5] }
6
+
7
+ let(:mixed_array) { [3, 4.0, 5, 2.0, 3, 1.0] }
8
+
9
+ describe '#mean' do
10
+
11
+ context 'with a array of integers' do
12
+ it 'should calculate the correct mean' do
13
+ expect(mean(int_array)).to eq(4)
14
+ end
15
+ end
16
+
17
+ context 'with a mixed array of integers and floats' do
18
+ it 'should calculate the correct mean' do
19
+ expect(mean(mixed_array)).to eq(3)
20
+ end
21
+ end
22
+
23
+ context 'with a empty array' do
24
+ it 'should give back a nil' do
25
+ expect(mean([])).to eq(nil)
26
+ end
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,67 @@
1
+ require 'spec_helper'
2
+
3
+ describe Average do
4
+
5
+ let(:int_array) { [3, 4, 5] }
6
+
7
+ let(:array) { [1, 1, 2, 2, 3, 4] }
8
+
9
+ let(:mixed_array) { [3, 4.0, 5, 2.0, 3, 1.0] }
10
+
11
+ let(:array_to_average) { [3, 4, 7, 8, 10, 12] }
12
+
13
+ let(:mode_array_not_pair) { [1, 1, 2, 2, 3, 4, 5] }
14
+
15
+ describe '#median' do
16
+
17
+ context 'with a array of integers' do
18
+ it 'should calculate the correct median' do
19
+ expect(median(int_array)).to eq(4)
20
+ end
21
+ end
22
+
23
+ context 'with a mixed array of integers and floats' do
24
+ it 'should calculate the correct median' do
25
+ expect(median(mixed_array)).to eq(3)
26
+ end
27
+ end
28
+
29
+ context 'with an empty array' do
30
+ it 'should gives back a nil' do
31
+ expect(median([])).to eq(nil)
32
+ end
33
+ end
34
+
35
+ end
36
+
37
+ describe '#get_median' do
38
+ context 'with an even array' do
39
+ it 'should give back the middle value' do
40
+ expect(get_median(array)).to eq(2.0)
41
+ end
42
+ end
43
+
44
+ context 'with a _not_ even array' do
45
+ it 'should give back the the element on the middle' do
46
+ expect(get_median(mode_array_not_pair)).to eq(2)
47
+ end
48
+ end
49
+ end
50
+
51
+ describe '#middle_item' do
52
+ context 'with a sorted array like [1,1,2,2,3,4]' do
53
+ it 'should give back the middle value' do
54
+ expect(middle_item(array)).to eq(3)
55
+ end
56
+ end
57
+ end
58
+
59
+ describe '#middle_items_to_average' do
60
+ context 'with an even array' do
61
+ it 'should give back two middle items' do
62
+ expect(middle_items_to_average(array_to_average)).to eq([7, 8])
63
+ end
64
+ end
65
+ end
66
+
67
+ end
@@ -0,0 +1,87 @@
1
+ require 'spec_helper'
2
+
3
+ describe Average do
4
+
5
+ let(:int_array) { [3,4,5] }
6
+
7
+ let(:mode_array) { [1,1,2,2,3,4] }
8
+
9
+ let(:mixed_array) { [3, 4.0, 5, 2.0, 3, 1.0] }
10
+
11
+ describe '#mode' do
12
+
13
+ context 'with a array of integers' do
14
+ it 'should calculate the correct mode' do
15
+ expect(mode(mode_array)).to eq([1, 2])
16
+ end
17
+ end
18
+
19
+ context 'with a empty array' do
20
+ it 'should return a empty array' do
21
+ expect(mode([])).to eq(nil)
22
+ end
23
+ end
24
+
25
+ context 'with a nil object' do
26
+ it 'should return a empty array' do
27
+ expect(mode([])).to eq(nil)
28
+ end
29
+ end
30
+
31
+ end
32
+
33
+ describe '#unique_mode' do
34
+
35
+ context 'with a array of integers' do
36
+ it 'should calculate the correct mode returning only one mode' do
37
+ expect(unique_mode(mode_array)).to eq(1)
38
+ end
39
+ end
40
+
41
+ context 'with a empty array' do
42
+ it 'should return nil' do
43
+ expect(unique_mode([])).to eq(nil)
44
+ end
45
+ end
46
+
47
+ context 'with a nil object' do
48
+ it 'should return nil' do
49
+ expect(unique_mode(nil)).to eq(nil)
50
+ end
51
+ end
52
+
53
+
54
+ end
55
+
56
+ describe '#repetition_hash' do
57
+
58
+ context 'should return a hash' do
59
+ subject { repetition_hash(mode_array) }
60
+ it 'with hash_result key' do
61
+ expect(subject).to include :hash_result
62
+ end
63
+
64
+ it 'with hash_result key' do
65
+ expect(subject[:hash_result]).to eql( { 1 => 2, 2 => 2, 3 => 1, 4 => 1 } )
66
+ end
67
+
68
+ it 'with max_repetition key' do
69
+ expect(subject).to include :max_repetition
70
+ end
71
+ end
72
+
73
+ context 'with a empty array' do
74
+ it 'should return nil' do
75
+ expect(repetition_hash([])).to eql(nil)
76
+ end
77
+ end
78
+
79
+ context 'with a empty array' do
80
+ it 'should return nil' do
81
+ expect(repetition_hash(nil)).to eql(nil)
82
+ end
83
+ end
84
+
85
+ end
86
+
87
+ end
@@ -0,0 +1 @@
1
+ require 'average'
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: average
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Fernando
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-02-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Find the arithmethic mean/mode/median of an array of numbers .
63
+ email:
64
+ - fgonzalezaguilera@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - average.gemspec
75
+ - lib/average.rb
76
+ - lib/average/average_helper.rb
77
+ - lib/average/mean.rb
78
+ - lib/average/median.rb
79
+ - lib/average/mode.rb
80
+ - lib/average/version.rb
81
+ - spec/average_helper_spec.rb
82
+ - spec/average_spec.rb
83
+ - spec/mean_spec.rb
84
+ - spec/median_spec.rb
85
+ - spec/mode_spec.rb
86
+ - spec/spec_helper.rb
87
+ homepage: ''
88
+ licenses:
89
+ - MIT
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ! '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 1.8.23
109
+ signing_key:
110
+ specification_version: 3
111
+ summary: Find the arithmethic mean/mode/median of an array of numbers .
112
+ test_files:
113
+ - spec/average_helper_spec.rb
114
+ - spec/average_spec.rb
115
+ - spec/mean_spec.rb
116
+ - spec/median_spec.rb
117
+ - spec/mode_spec.rb
118
+ - spec/spec_helper.rb