boxxer 0.1.4 → 0.1.5
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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +3 -5
- data/lib/boxxer/container.rb +6 -6
- data/lib/boxxer/handler.rb +13 -8
- data/lib/boxxer/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d7737f168e9e937115ec24749076825f5fc4f887d114622e39587da103c6c9b0
|
4
|
+
data.tar.gz: ba7ee022f7852be525ef5c43ca25adfd8ec80d9a47db25da038f36fe0894ef2e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0d13897fd62c410b65d5101bc280c5ce2ffb8070015c22296bb5794d19bbc15a397695855acca9e352f9550588ff915a00f3fc6c84384ec583f112f620e65033
|
7
|
+
data.tar.gz: 02a5c9778d2a6a34fe6fff2c8c3dfa226e795008d674bc19633e5fc0a515dcb0cd8cbca6d70732bddeebacb3c42b9627c1e635cd5b42b531b3bd29254a6dc194
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -10,15 +10,13 @@ irb
|
|
10
10
|
```
|
11
11
|
|
12
12
|
```
|
13
|
+
require 'securerandom'
|
13
14
|
require 'boxxer'
|
14
|
-
|
15
|
-
20.times { weights << rand(0.5..1).round(3) }
|
15
|
+
contents = 20.times.with_object([]) { |_, obj| obj.push(item: SecureRandom.alphanumeric(10), weight: rand(0.5..1).round(3)) }
|
16
16
|
containers = [{ length: 47, width: 38, height: 10, tare_weight: 0.019, net_limit: 0.481 },
|
17
17
|
{ length: 40, width: 28, height: 13, tare_weight: 0.34, net_limit: 0.66 },
|
18
18
|
{ length: 40, width: 28, height: 80, tare_weight: 0.1, net_limit: 0.7 },
|
19
19
|
{ length: 40, width: 28, height: 18, tare_weight: 0.5, net_limit: 1.5 },
|
20
20
|
{ length: 40, width: 28, height: 20, tare_weight: 0.52, net_limit: 2.48 }]
|
21
|
-
|
22
|
-
arranger.call
|
23
|
-
arranger.containers
|
21
|
+
boxxer = Boxxer.call(containers: containers, contents: contents)
|
24
22
|
```
|
data/lib/boxxer/container.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module Boxxer
|
2
2
|
class Container
|
3
|
-
attr_reader :tare_weight, :
|
4
|
-
attr_writer :
|
3
|
+
attr_reader :tare_weight, :width, :height, :length
|
4
|
+
attr_writer :contents
|
5
5
|
|
6
6
|
def initialize(width:, height:, length:, tare_weight:, net_limit:)
|
7
7
|
@width = width
|
@@ -9,11 +9,11 @@ module Boxxer
|
|
9
9
|
@length = length
|
10
10
|
@net_limit = net_limit
|
11
11
|
@tare_weight = tare_weight
|
12
|
-
@
|
12
|
+
@contents = []
|
13
13
|
end
|
14
14
|
|
15
15
|
def net_weight
|
16
|
-
|
16
|
+
@contents.sum { |content| content[:weight] }.truncate(3)
|
17
17
|
end
|
18
18
|
|
19
19
|
def gross_weight
|
@@ -24,8 +24,8 @@ module Boxxer
|
|
24
24
|
@net_limit >= net_weight + weight
|
25
25
|
end
|
26
26
|
|
27
|
-
def
|
28
|
-
@
|
27
|
+
def add_content(content)
|
28
|
+
@contents.push(content)
|
29
29
|
end
|
30
30
|
end
|
31
31
|
end
|
data/lib/boxxer/handler.rb
CHANGED
@@ -4,16 +4,17 @@ module Boxxer
|
|
4
4
|
class Handler
|
5
5
|
attr_reader :containers
|
6
6
|
|
7
|
-
def initialize(containers:,
|
7
|
+
def initialize(containers:, contents:)
|
8
8
|
@available_containers = containers.sort { |container| container[:net_limit] }.reverse
|
9
9
|
@largest_container = @available_containers.last
|
10
|
-
@
|
10
|
+
@contents = contents.sort_by { |content| content[:weight] }.reverse
|
11
11
|
@containers = []
|
12
12
|
end
|
13
13
|
|
14
14
|
def call
|
15
|
-
raise Error, "Largest container net_limit is #{@largest_container[:net_limit]}, but maximum weight is #{
|
16
|
-
|
15
|
+
raise Error, "Largest container net_limit is #{@largest_container[:net_limit]}, but maximum weight is #{content_max_weight}" if invalid_options?
|
16
|
+
|
17
|
+
until @contents.empty? do
|
17
18
|
container = Container.new(matching_container)
|
18
19
|
complete_container(container)
|
19
20
|
@containers << container
|
@@ -37,17 +38,21 @@ module Boxxer
|
|
37
38
|
|
38
39
|
def complete_container(container)
|
39
40
|
loop do
|
40
|
-
|
41
|
-
|
41
|
+
fittable_conteiner = @contents.find { |content| container.fittable?(content[:weight]) }
|
42
|
+
fittable_conteiner.nil? ? break : container.add_content(@contents.delete_at(@contents.index(fittable_conteiner)))
|
42
43
|
end
|
43
44
|
end
|
44
45
|
|
46
|
+
def content_max_weight
|
47
|
+
@contents.max { |c| c[:weight] }
|
48
|
+
end
|
49
|
+
|
45
50
|
def invalid_options?
|
46
|
-
|
51
|
+
content_max_weight[:weight] > @largest_container[:net_limit]
|
47
52
|
end
|
48
53
|
|
49
54
|
def matching_container
|
50
|
-
@available_containers.find { |container| container[:net_limit] >= @
|
55
|
+
@available_containers.find { |container| container[:net_limit] >= @contents.sum { |content| content[:weight] } } || @largest_container
|
51
56
|
end
|
52
57
|
end
|
53
58
|
end
|
data/lib/boxxer/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: boxxer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dmitry Sivoglaz
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-07-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: awesome_print
|