smart_rounding 0.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.
- checksums.yaml +7 -0
- data/lib/smart_rounding.rb +41 -0
- metadata +45 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7b28ac81bec8bede725379cf8546f54b902cfb7e
|
4
|
+
data.tar.gz: 2135cc0260077d7de14ccd909f13e19394d477a7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b37aaffd048491cfd824f91df477568d6b9462c9f0f65ff36eb22dac87cc242a428367ebe323273ce8913ff8a8fee0cc396cf1f5d63362e6e655f2e5334eb48d
|
7
|
+
data.tar.gz: cf52e40805d284f82366e381fa550d82d4098133fe42dc1eee7b69e0d69a2cab0a4b5b63e537d5c62d8789d5573b6458973cdc70ca8143a252c420274252dcd3
|
@@ -0,0 +1,41 @@
|
|
1
|
+
class SmartRounding
|
2
|
+
def initialize array
|
3
|
+
@array = array
|
4
|
+
end
|
5
|
+
|
6
|
+
def rounded_sum
|
7
|
+
array.inject(:+).round()
|
8
|
+
end
|
9
|
+
|
10
|
+
def rounded_array
|
11
|
+
array.each_with_index.map{ |number, index| indexes_of_ceil_numbers.include?(index) ? number.ceil : number.floor }
|
12
|
+
end
|
13
|
+
|
14
|
+
def rounds_equal?
|
15
|
+
rounded_array.inject(:+) == rounded_sum
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def fractionals
|
21
|
+
array.map{|number| number.modulo(1)}
|
22
|
+
end
|
23
|
+
|
24
|
+
def sum_of_fractionals
|
25
|
+
fractionals.inject(:+)
|
26
|
+
end
|
27
|
+
|
28
|
+
def ceil_numbers_count
|
29
|
+
sum_of_fractionals.round(0)
|
30
|
+
end
|
31
|
+
|
32
|
+
def ceil_numbers
|
33
|
+
fractionals.each_with_index.max(ceil_numbers_count)
|
34
|
+
end
|
35
|
+
|
36
|
+
def indexes_of_ceil_numbers
|
37
|
+
ceil_numbers.map(&:last)
|
38
|
+
end
|
39
|
+
|
40
|
+
attr_reader :array
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: smart_rounding
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Anatolii Kryvishyn
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-07-03 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Sum of rounded elements in the array should be equal the rounded sum
|
14
|
+
of elements
|
15
|
+
email: tolik303@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/smart_rounding.rb
|
21
|
+
homepage: http://rubygems.org/gems/smart_rounding
|
22
|
+
licenses:
|
23
|
+
- MIT
|
24
|
+
metadata: {}
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 2.6.12
|
42
|
+
signing_key:
|
43
|
+
specification_version: 4
|
44
|
+
summary: Round IT!
|
45
|
+
test_files: []
|