divvy_up 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -1
- data/README.md +11 -5
- data/lib/divvy_up/list.rb +17 -1
- data/lib/divvy_up/version.rb +1 -1
- data/spec/divvy_up/list_spec.rb +10 -9
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b70749fb098ddfabaf998c6db2cbdf1087cb03f2
|
4
|
+
data.tar.gz: 374ec2326dcae01d1704744e4d10d7df0bc77b44
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8b8abe54e48cbbdeaed396899e2ccc7a4a093d2c6017ed12f8d7e2e0c7718f6116634e230a427ea6d4c9ee590faf0b341e0160ee2c951b5f4cb63f0c64cba2d5
|
7
|
+
data.tar.gz: 3c4ec687c85b507334dbcabb479a29224e85f5e8a9301cb1cf987e9bc4974c73cf5b67aaf352a4c5541c2d290ba2758adf0920dd4d29b1f5b12cb834c19c1ff6
|
data/CHANGELOG.md
CHANGED
@@ -4,6 +4,11 @@ This project adheres to [Semantic Versioning](http://semver.org/).
|
|
4
4
|
|
5
5
|
## [Unreleased]
|
6
6
|
|
7
|
+
## [0.2.0] - 2015-08-04
|
8
|
+
### Added
|
9
|
+
- Allow specifying item price via hash attribute
|
10
|
+
- Allow specifying quantity of item via hash attribute
|
11
|
+
|
7
12
|
## [0.1.2] - 2015-07-09
|
8
13
|
### Fixed
|
9
14
|
- Resolve potential infinite loop in `#split` occurring when no valid solution is possible using "permute" technique
|
@@ -19,7 +24,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
|
|
19
24
|
## 0.0.1 - 2015-03-09
|
20
25
|
- Initial release
|
21
26
|
|
22
|
-
[unreleased]: https://github.com/djpowers/divvy_up/compare/v0.
|
27
|
+
[unreleased]: https://github.com/djpowers/divvy_up/compare/v0.2.0...HEAD
|
28
|
+
[0.2.0]: https://github.com/djpowers/divvy_up/compare/v0.1.2...v0.2.0
|
23
29
|
[0.1.2]: https://github.com/djpowers/divvy_up/compare/v0.1.1...v0.1.2
|
24
30
|
[0.1.1]: https://github.com/djpowers/divvy_up/compare/v0.1.0...v0.1.1
|
25
31
|
[0.1.0]: https://github.com/djpowers/divvy_up/compare/v0.0.1...v0.1.0
|
data/README.md
CHANGED
@@ -27,10 +27,10 @@ Or install it yourself as:
|
|
27
27
|
|
28
28
|
```ruby
|
29
29
|
shopping_list = {
|
30
|
-
orange_juice: 3,
|
30
|
+
orange_juice: { price: 3, quantity: 2 },
|
31
31
|
lettuce: 7,
|
32
32
|
strawberries: 3,
|
33
|
-
eggs: 2.79,
|
33
|
+
eggs: { price: 2.79 },
|
34
34
|
carrots: 2.5,
|
35
35
|
onion: 1.25,
|
36
36
|
tomato: 1.25,
|
@@ -44,12 +44,18 @@ shopping_list = {
|
|
44
44
|
DivvyUp::List.new(shopping_list).split(3)
|
45
45
|
# =>
|
46
46
|
# [
|
47
|
-
# [{:
|
48
|
-
# [{:
|
49
|
-
# [{:
|
47
|
+
# [{:eggs=>2.79, :carrots=>2.5, :onion=>1.25, :butter=>2.69, :pepper=>2}, 11.23],
|
48
|
+
# [{:tomato=>1.25, :blueberries=>3.99, :orange_juice_1=>3, :orange_juice_2=>3}, 11.24],
|
49
|
+
# [{:lettuce=>7, :pasta_sauce=>2.5, :celery=>1.69}, 11.19]
|
50
50
|
# ]
|
51
51
|
```
|
52
52
|
|
53
|
+
Pass a hash as an argument when creating a new List, where the keys are the item
|
54
|
+
names, and the values are the prices, as an integer or float.
|
55
|
+
|
56
|
+
Optionally, you may specify the price in an attributes hash. If you have more
|
57
|
+
than one of an item, you can specify the quantity here as well.
|
58
|
+
|
53
59
|
Output of `#split` method consists of an array of arrays, where each subarray
|
54
60
|
is a hash of items and the total value of those items.
|
55
61
|
|
data/lib/divvy_up/list.rb
CHANGED
@@ -3,7 +3,7 @@ module DivvyUp
|
|
3
3
|
attr_reader :items
|
4
4
|
|
5
5
|
def initialize(items)
|
6
|
-
@items = items
|
6
|
+
@items = parse_items(items)
|
7
7
|
end
|
8
8
|
|
9
9
|
def split(groups)
|
@@ -14,6 +14,22 @@ module DivvyUp
|
|
14
14
|
|
15
15
|
private
|
16
16
|
|
17
|
+
def parse_items(items)
|
18
|
+
quantified_items = {}
|
19
|
+
items.each do |item, attributes|
|
20
|
+
next if attributes.is_a? Numeric
|
21
|
+
items[item] = attributes[:price]
|
22
|
+
if attributes[:quantity]
|
23
|
+
attributes[:quantity].times do |index|
|
24
|
+
item_name = (item.to_s + '_' + (index + 1).to_s).to_sym
|
25
|
+
quantified_items[item_name] = attributes[:price]
|
26
|
+
items.delete(item)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
items.merge(quantified_items)
|
31
|
+
end
|
32
|
+
|
17
33
|
def permute
|
18
34
|
sublists = sublist_permutations
|
19
35
|
price_differences = sublist_price_differences(sublists)
|
data/lib/divvy_up/version.rb
CHANGED
data/spec/divvy_up/list_spec.rb
CHANGED
@@ -1,10 +1,9 @@
|
|
1
1
|
module DivvyUp
|
2
2
|
describe List do
|
3
3
|
let(:shopping_list) { {
|
4
|
-
orange_juice: 3,
|
4
|
+
orange_juice: { price: 3, quantity: 2 },
|
5
5
|
lettuce: 7,
|
6
|
-
|
7
|
-
eggs: 2.79,
|
6
|
+
eggs: { price: 2.79 },
|
8
7
|
carrots: 2.5,
|
9
8
|
onion: 1.25,
|
10
9
|
tomato: 1.25,
|
@@ -27,7 +26,9 @@ module DivvyUp
|
|
27
26
|
|
28
27
|
it "splits a list into one group" do
|
29
28
|
list = DivvyUp::List.new(shopping_list)
|
30
|
-
expect(list.split(1)).to eql(
|
29
|
+
expect(list.split(1)).to eql(
|
30
|
+
[{:lettuce=>7, :eggs=>2.79, :carrots=>2.5, :onion=>1.25, :tomato=>1.25, :blueberries=>3.99, :butter=>2.69, :pasta_sauce=>2.5, :pepper=>2, :celery=>1.69, :orange_juice_1=>3, :orange_juice_2=>3}]
|
31
|
+
)
|
31
32
|
end
|
32
33
|
|
33
34
|
it "splits a two-item list into two groups" do
|
@@ -42,13 +43,13 @@ module DivvyUp
|
|
42
43
|
[{bananas: 2.40, pears: 3.20}, 5.60]])
|
43
44
|
end
|
44
45
|
|
45
|
-
it "splits a list into three groups" do
|
46
|
+
it "splits a list with attributes hash into three groups" do
|
46
47
|
list = DivvyUp::List.new(shopping_list)
|
47
48
|
expect(list.split(3)).to eql(
|
48
49
|
[
|
49
|
-
[{
|
50
|
-
[{
|
51
|
-
[{
|
50
|
+
[{:eggs=>2.79, :carrots=>2.5, :onion=>1.25, :butter=>2.69, :pepper=>2}, 11.23],
|
51
|
+
[{:tomato=>1.25, :blueberries=>3.99, :orange_juice_1=>3, :orange_juice_2=>3}, 11.24],
|
52
|
+
[{:lettuce=>7, :pasta_sauce=>2.5, :celery=>1.69}, 11.19]
|
52
53
|
]
|
53
54
|
)
|
54
55
|
end
|
@@ -88,7 +89,7 @@ module DivvyUp
|
|
88
89
|
)
|
89
90
|
end
|
90
91
|
|
91
|
-
it "splits a
|
92
|
+
it "splits a list where permute technique does not return a valid result" do
|
92
93
|
shopping_list = { orange_juice_1: 3, orange_juice_2: 3, eggs_dozen: 2.99, spring_mix_16_oz: 6.99, bacon: 4.99, pasta_sauce: 2.50, blueberries: 3.99, frozen_strawberries: 2.99, olive_oil: 8.99, paper_towels: 1.59, toilet_paper: 1.99, aluminum_foil: 3 }
|
93
94
|
list = DivvyUp::List.new(shopping_list)
|
94
95
|
expect(list.split(3)).to eql(
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: divvy_up
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dave Powers
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-08-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|