divvy_up 0.1.2 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cb8031716a0ff8be7b2330f02f3729980a2f0611
4
- data.tar.gz: f7e589444af474686c6ab06ca77d9dd01becdd69
3
+ metadata.gz: b70749fb098ddfabaf998c6db2cbdf1087cb03f2
4
+ data.tar.gz: 374ec2326dcae01d1704744e4d10d7df0bc77b44
5
5
  SHA512:
6
- metadata.gz: eacc87d816ed1236597a458a0a2ef90578647bd6529ce88aeedbb91e92ba788ae8b10f6e6a6dab071eb497e4525cf6247f04bf502d99d5262e032f9c81bc6a67
7
- data.tar.gz: ce779873f08a334cc3f9476c27c487e2e374d034160826115852fe96aba95703428e128b255c659f238d9985f5c8c799c09d5949de69bf1867f8e0bbabfb4bdf
6
+ metadata.gz: 8b8abe54e48cbbdeaed396899e2ccc7a4a093d2c6017ed12f8d7e2e0c7718f6116634e230a427ea6d4c9ee590faf0b341e0160ee2c951b5f4cb63f0c64cba2d5
7
+ data.tar.gz: 3c4ec687c85b507334dbcabb479a29224e85f5e8a9301cb1cf987e9bc4974c73cf5b67aaf352a4c5541c2d290ba2758adf0920dd4d29b1f5b12cb834c19c1ff6
@@ -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.1.2...HEAD
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
- # [{:orange_juice=>3, :eggs=>2.79, :carrots=>2.5, :onion=>1.25, :celery=>1.69}, 11.23],
48
- # [{:lettuce=>7, :strawberries=>3, :tomato=>1.25}, 11.25],
49
- # [{:blueberries=>3.99, :butter=>2.69, :pasta_sauce=>2.5, :pepper=>2}, 11.18]
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
 
@@ -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)
@@ -1,3 +1,3 @@
1
1
  module DivvyUp
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -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
- strawberries: 3,
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([shopping_list])
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
- [{orange_juice: 3, eggs: 2.79, carrots: 2.5, onion: 1.25, celery: 1.69}, 11.23],
50
- [{lettuce: 7, strawberries: 3, tomato: 1.25}, 11.25],
51
- [{blueberries: 3.99, butter: 2.69, pasta_sauce: 2.5, pepper: 2}, 11.18]
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 large list into three groups" do
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.1.2
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-07-10 00:00:00.000000000 Z
11
+ date: 2015-08-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler