array_arithmetic 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 19d3fc33ed00ad3be2395498f6a347d2591311e5
4
+ data.tar.gz: 7096f3eeed87c320573d357e502ce4808a7411b9
5
+ SHA512:
6
+ metadata.gz: 9c18284274a084b7d2274ffe1fcbcbbd41f885512994e85d7f3b1af35c03c52198434706d40f3503e16dc8fa9b0f55d0245b240af9be861787cbbcd2c011d416
7
+ data.tar.gz: 517520ae5139fd5b72f10cf0506bc000c2ecac7feeb36a9098399bef25095f2545b0e480230be829b4268a1b0d6ab70bd99a102b5afb61744d1f30b84abb0496
Binary file
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.4.1
7
+ before_install: gem install bundler -v 2.0.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in array_arithmetic.gemspec
4
+ gemspec
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ array_arithmetic (1.0.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (10.5.0)
11
+ rspec (3.8.0)
12
+ rspec-core (~> 3.8.0)
13
+ rspec-expectations (~> 3.8.0)
14
+ rspec-mocks (~> 3.8.0)
15
+ rspec-core (3.8.1)
16
+ rspec-support (~> 3.8.0)
17
+ rspec-expectations (3.8.4)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.8.0)
20
+ rspec-mocks (3.8.1)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.8.0)
23
+ rspec-support (3.8.2)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ array_arithmetic!
30
+ bundler (~> 2.0)
31
+ rake (~> 10.0)
32
+ rspec (~> 3.0)
33
+
34
+ BUNDLED WITH
35
+ 2.0.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 C.J. Adeszko
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,132 @@
1
+ # Array_Arithmetic
2
+
3
+ [![Build Status](https://travis-ci.com/CJAdeszko/array_arithmetic.svg?branch=master)](https://travis-ci.com/CJAdeszko/array_arithmetic)
4
+
5
+ Array_Arithmetic provides users the ability to perform common math functions on entire arrays in Ruby. This gem provides methods for the following math functions:
6
+
7
+ - Addition
8
+ - Subtraction
9
+ - Multiplication
10
+ - Division
11
+ - Remainders
12
+ - Exponents
13
+ - Square
14
+ - Square root
15
+
16
+ ## Installation
17
+
18
+ Add this line to your application's Gemfile:
19
+
20
+ ```ruby
21
+ gem 'array_arithmetic'
22
+ ```
23
+
24
+ And then execute:
25
+
26
+ $ bundle
27
+
28
+ Or install it yourself as:
29
+
30
+ $ gem install array_arithmetic
31
+
32
+ ## Usage
33
+
34
+ The `array_arithmetic` gem provides the following methods for use:
35
+
36
+ - add()
37
+ - subtract()
38
+ - multiply()
39
+ - divide()
40
+ - remainder()
41
+ - exponent()
42
+ - square()
43
+ - square_root()
44
+
45
+ To use a method within your code, simply include the module at the top of the file:
46
+
47
+ ```ruby
48
+ include ArrayArithmetic
49
+ ```
50
+
51
+ Then, you are able to call methods as follows:
52
+
53
+ ```ruby
54
+ arr_one = [1, 2, 3, 4, 5]
55
+ arr_two = [9, 8, 7, 6, 5]
56
+
57
+ add(arr_one, arr_two)
58
+ #returns [10, 10, 10, 10, 10]
59
+
60
+ subtract(arr_one, arr_two)
61
+ #returns [-8, -6, -4, -2, 0]
62
+
63
+ multiply(arr_one, arr_two)
64
+ #returns [9, 16, 21, 24, 25]
65
+
66
+ exponent(arr_one, arr_two)
67
+ #returns [1, 256, 2187, 4096, 3125]
68
+ ```
69
+
70
+ Note that both the `divide` and `remainder` methods return values as a float, and have an optional third argument `sig_dig`, which defaults to 2. If desired, an integer can be passed as a third argument to the method to change the number of significant digits the method returns:
71
+
72
+ ```ruby
73
+ arr_one = [1, 2, 3, 4, 5]
74
+ arr_two = [9, 8, 7, 6, 5]
75
+
76
+ divide(arr_one, arr_two)
77
+ #returns [0.11, 0.25, 0.43, 0.67, 1.0]
78
+
79
+ divide(arr_one, arr_two, 4)
80
+ #returns [0.1111, 0.25, 0.4286, 0.6667, 1.0]
81
+
82
+ remainder(arr_one, arr_two)
83
+ #returns [1.0, 2.0, 3.0, 4.0, 0.0]
84
+
85
+ remainder(arr_one, 0.405, 4)
86
+ #returns [0.19, 0.38, 0.165, 0.355, 0.14]
87
+ ```
88
+
89
+ Note that both the `square` and `square_root` methods take in only one array as an argument.
90
+
91
+ ```ruby
92
+ arr_one = [1, 2, 3, 4, 5]
93
+ arr_two = [9, 8, 7, 6, 5]
94
+
95
+ square(arr_one)
96
+ #returns [1, 4, 9, 16, 25]
97
+
98
+ square_root(arr_two)
99
+ #returns [3.0, 2.8284271247461903, 2.6457513110645907, 2.449489742783178, 2.23606797749979]
100
+ ```
101
+
102
+ If arrays of unequal length are passed into any method, the values of shorter of the two arrays are recycled so that an array matching the length of the longer array is returned:
103
+
104
+ ```ruby
105
+ arr_one = [1, 2, 3, 4, 5]
106
+ arr_two = [9, 8, 7, 6, 5, 4, 3, 2, 1]
107
+
108
+ #arr_one values are recycled making arr_one = [1, 2, 3, 4, 5, 1, 2, 3, 4]
109
+
110
+ add(arr_one, arr_two)
111
+ #returns [10, 10, 10, 10, 10, 5, 5, 5, 5]
112
+ ```
113
+
114
+ Additionally, note that the first argument passed to any of the methods MUST be an array, otherwise, the method will raise an error. However, the second argument can be either an array or a single number which will be used in the methods calculations:
115
+
116
+ ```ruby
117
+ arr_one = [1, 2, 3, 4, 5]
118
+
119
+ multiply(arr_one, 5)
120
+ #returns [5, 10, 15, 20, 25]
121
+
122
+ divide(arr_one, 0.5)
123
+ #returns [2.0, 4.0, 6.0, 8.0, 10.0]
124
+ ```
125
+
126
+ ## Contributing
127
+
128
+ Bug reports and pull requests are welcome on GitHub at https://github.com/CJAdeszko/array_arithmetic.
129
+
130
+ ## License
131
+
132
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,30 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "array_arithmetic/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "array_arithmetic"
7
+ spec.version = ArrayArithmetic::VERSION
8
+ spec.authors = ["C.J. Adeszko"]
9
+
10
+ spec.summary = "Provides users with methods for performing common math functions on arrays."
11
+ spec.homepage = "https://github.com/CJAdeszko/array_arithmetic"
12
+ spec.license = "MIT"
13
+
14
+ spec.metadata["homepage_uri"] = "https://github.com/CJAdeszko/array_arithmetic"
15
+ spec.metadata["source_code_uri"] = "https://github.com/CJAdeszko/array_arithmetic"
16
+ spec.metadata["documentation_uri"] = "https://github.com/CJAdeszko/array_arithmetic"
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_development_dependency "bundler", "~> 2.0"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec", "~> 3.0"
30
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "array_arithmetic"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,226 @@
1
+ require "array_arithmetic/version"
2
+
3
+ module ArrayArithmetic
4
+ class Error < StandardError; end
5
+
6
+ #Return an argument as an array for use with arithmetic methods
7
+ def make_array(arg)
8
+ return [arg]
9
+ end
10
+
11
+ #Takes in two arrays of unequal length passed to the arithmetic method
12
+ #Recycles values from the shorter array
13
+ #Returns an array containing two arrays of equal length
14
+ def update_array_length(arr_one, arr_two)
15
+ if arr_one.length < arr_two.length
16
+ adjusted_arr_one = []
17
+ arr_two.each_with_index do |value, index|
18
+ if arr_one.length == 1
19
+ adjusted_arr_one << arr_one[0]
20
+ elsif index >= arr_one.length
21
+ adjusted_arr_one << arr_one[index - arr_one.length]
22
+ else
23
+ adjusted_arr_one << arr_one[index]
24
+ end
25
+ end
26
+ return adjusted_arr_one, arr_two
27
+ elsif arr_one.length > arr_two.length
28
+ adjusted_arr_two = []
29
+ arr_one.each_with_index do |value, index|
30
+ if arr_two.length == 1
31
+ adjusted_arr_two << arr_two[0]
32
+ elsif index >= arr_two.length
33
+ adjusted_arr_two << arr_two[index - arr_two.length]
34
+ else
35
+ adjusted_arr_two << arr_two[index]
36
+ end
37
+ end
38
+ return arr_one, adjusted_arr_two
39
+ end
40
+ end
41
+
42
+
43
+ #Adds the values of corresponding indices of two arrays
44
+ def add(arr_one, arr_two)
45
+ raise ArgumentError, 'First argument is not an array' unless arr_one.is_a? Array
46
+
47
+ result = []
48
+
49
+ if arr_two.class != Array
50
+ arr_two = make_array(arr_two)
51
+ end
52
+
53
+ if arr_one.length != arr_two.length
54
+ length_matched_arrays = update_array_length(arr_one, arr_two)
55
+
56
+ length_matched_arrays[0].each_with_index do |value, index|
57
+ result << value + length_matched_arrays[1][index]
58
+ end
59
+ else
60
+ arr_one.each_with_index do |value, index|
61
+ result << value + arr_two[index]
62
+ end
63
+ end
64
+
65
+ return result
66
+ end
67
+
68
+
69
+ #Subtracts the values of corresponding indices of two arrays
70
+ def subtract(arr_one, arr_two)
71
+ raise ArgumentError, 'First argument is not an array' unless arr_one.is_a? Array
72
+
73
+ result = []
74
+
75
+ if arr_two.class != Array
76
+ arr_two = make_array(arr_two)
77
+ end
78
+
79
+ if arr_one.length != arr_two.length
80
+ length_matched_arrays = update_array_length(arr_one, arr_two)
81
+
82
+ length_matched_arrays[0].each_with_index do |value, index|
83
+ result << value - length_matched_arrays[1][index]
84
+ end
85
+ else
86
+ arr_one.each_with_index do |value, index|
87
+ result << value - arr_two[index]
88
+ end
89
+ end
90
+
91
+ return result
92
+ end
93
+
94
+
95
+ #Multiplies the values of corresponding indices of two arrays
96
+ def multiply(arr_one, arr_two)
97
+ raise ArgumentError, 'First argument is not an array' unless arr_one.is_a? Array
98
+
99
+ result = []
100
+
101
+ if arr_two.class != Array
102
+ arr_two = make_array(arr_two)
103
+ end
104
+
105
+ if arr_one.length != arr_two.length
106
+ length_matched_arrays = update_array_length(arr_one, arr_two)
107
+
108
+ length_matched_arrays[0].each_with_index do |value, index|
109
+ result << value * length_matched_arrays[1][index]
110
+ end
111
+ else
112
+ arr_one.each_with_index do |value, index|
113
+ result << value * arr_two[index]
114
+ end
115
+ end
116
+
117
+ return result
118
+ end
119
+
120
+
121
+ #Divides the values of corresponding indices of two arrays
122
+ #Returns values as a float
123
+ def divide(arr_one, arr_two, sig_dig=2)
124
+ raise ArgumentError, 'First argument is not an array' unless arr_one.is_a? Array
125
+
126
+ result = []
127
+
128
+ if arr_two.class != Array
129
+ arr_two = make_array(arr_two)
130
+ end
131
+
132
+ if arr_one.length != arr_two.length
133
+ length_matched_arrays = update_array_length(arr_one, arr_two)
134
+
135
+ length_matched_arrays[0].each_with_index do |value, index|
136
+ if length_matched_arrays[1][index] == 0
137
+ result << "Divide by 0 error"
138
+ else
139
+ result << (value.to_f / length_matched_arrays[1][index]).round(sig_dig)
140
+ end
141
+ end
142
+ else
143
+ arr_one.each_with_index do |value, index|
144
+ if arr_two[index] == 0
145
+ result << "Divide by 0 error"
146
+ else
147
+ result << (value.to_f / arr_two[index]).round(sig_dig)
148
+ end
149
+ end
150
+ end
151
+
152
+ return result
153
+ end
154
+
155
+
156
+ #Calculates the remainder of corresponding inidices of two arrays
157
+ def remainder(arr_one, arr_two, sig_dig=2)
158
+ raise ArgumentError, 'First argument is not an array' unless arr_one.is_a? Array
159
+
160
+ result = []
161
+
162
+ if arr_two.class != Array
163
+ arr_two = make_array(arr_two)
164
+ end
165
+
166
+ if arr_one.length != arr_two.length
167
+ length_matched_arrays = update_array_length(arr_one, arr_two)
168
+
169
+ length_matched_arrays[0].each_with_index do |value, index|
170
+ if length_matched_arrays[1][index] == 0
171
+ result << "Divide by 0 error"
172
+ else
173
+ result << (value.to_f % length_matched_arrays[1][index]).round(sig_dig)
174
+ end
175
+ end
176
+ else
177
+ arr_one.each_with_index do |value, index|
178
+ if arr_two[index] == 0
179
+ result << "Divide by 0 error"
180
+ else
181
+ result << (value.to_f % arr_two[index]).round(sig_dig)
182
+ end
183
+ end
184
+ end
185
+
186
+ return result
187
+ end
188
+
189
+
190
+ #Raises the values of arr_one to the power of the value at corresponding indices in arr_two
191
+ def exponent(arr_one, arr_two)
192
+ raise ArgumentError, 'First argument is not an array' unless arr_one.is_a? Array
193
+
194
+ result = []
195
+
196
+ if arr_two.class != Array
197
+ arr_two = make_array(arr_two)
198
+ end
199
+
200
+ if arr_one.length != arr_two.length
201
+ length_matched_arrays = update_array_length(arr_one, arr_two)
202
+
203
+ length_matched_arrays[0].each_with_index do |value, index|
204
+ result << value ** length_matched_arrays[1][index]
205
+ end
206
+ else
207
+ arr_one.each_with_index do |value, index|
208
+ result << value ** arr_two[index]
209
+ end
210
+ end
211
+
212
+ return result
213
+ end
214
+
215
+
216
+ #Squares the values of an array
217
+ def square(array)
218
+ return exponent(array, 2)
219
+ end
220
+
221
+
222
+ #Returns the square root of each value of an array
223
+ def square_root(array)
224
+ return exponent(array, 0.5)
225
+ end
226
+ end
@@ -0,0 +1,3 @@
1
+ module ArrayArithmetic
2
+ VERSION = "1.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: array_arithmetic
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - C.J. Adeszko
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-06-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - ".DS_Store"
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - array_arithmetic-0.1.0.gem
71
+ - array_arithmetic.gemspec
72
+ - bin/console
73
+ - bin/setup
74
+ - lib/array_arithmetic.rb
75
+ - lib/array_arithmetic/version.rb
76
+ homepage: https://github.com/CJAdeszko/array_arithmetic
77
+ licenses:
78
+ - MIT
79
+ metadata:
80
+ homepage_uri: https://github.com/CJAdeszko/array_arithmetic
81
+ source_code_uri: https://github.com/CJAdeszko/array_arithmetic
82
+ documentation_uri: https://github.com/CJAdeszko/array_arithmetic
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.6.14
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Provides users with methods for performing common math functions on arrays.
103
+ test_files: []