divvy_up 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 13df8fba53cef5c26b453bdfb56828dfcb70b36e
4
+ data.tar.gz: 58d128d044bbca50a8a76e07775e2e893265842b
5
+ SHA512:
6
+ metadata.gz: e5da9da7c428b2260b50931151c95609def33a8883707113572701df727dbb4b9e0e55d5e16b9ddcad28d173257e392614a2f2812e15008a97567296b39c4fae
7
+ data.tar.gz: 06e9f4ed191d2759ade0ca39cc348e800cc40924a82dc3a8613165b18a3e9febdb63645c292294a3feaf2f70967d95ef97abbee2ad8529bba69464cd5ceaa405
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in divvy_up.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Dave Powers
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # DivvyUp
2
+
3
+ A Ruby gem to divvy up a list of item prices into smaller groups,
4
+ for the purpose of splitting up purchases (somehwat) equally.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'divvy_up'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install divvy_up
21
+
22
+ ## Usage
23
+
24
+ ```ruby
25
+ shopping_list = {
26
+ orange_juice: 3,
27
+ lettuce: 7,
28
+ strawberries: 3,
29
+ eggs: 2.79,
30
+ carrots: 2.5,
31
+ onion: 1.25,
32
+ tomato: 1.25,
33
+ blueberries: 3.99,
34
+ butter: 2.69,
35
+ pasta_sauce: 2.5,
36
+ pepper: 2,
37
+ celery: 1.69
38
+ }
39
+
40
+ DivvyUp::List.new(shopping_list).split(3)
41
+ # =>
42
+ # [
43
+ # [{:orange_juice=>3, :eggs=>2.79, :carrots=>2.5, :onion=>1.25, :celery=>1.69}, 11.23],
44
+ # [{:lettuce=>7, :strawberries=>3, :tomato=>1.25}, 11.25],
45
+ # [{:blueberries=>3.99, :butter=>2.69, :pasta_sauce=>2.5, :pepper=>2}, 11.18]
46
+ # ]
47
+ ```
48
+
49
+ Output of `#split` method consists of an array of arrays, where each subarray
50
+ is a hash of items and the total value of those items.
51
+
52
+ ## Contributing
53
+
54
+ 1. Fork it ( https://github.com/djpowers/divvy_up/fork )
55
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
56
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
57
+ 4. Push to the branch (`git push origin my-new-feature`)
58
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/divvy_up.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'divvy_up/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "divvy_up"
8
+ spec.version = DivvyUp::VERSION
9
+ spec.authors = ["Dave Powers"]
10
+ spec.email = ["djpowers89@gmail.com"]
11
+ spec.summary = %q{Divvy up purchases to split amongst friends}
12
+ spec.description = %q{Divvy up purchases to split amongst friends}
13
+ spec.homepage = "https://github.com/djpowers/divvy_up"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "pry"
25
+ end
@@ -0,0 +1,92 @@
1
+ module DivvyUp
2
+ class List
3
+ attr_reader :items
4
+
5
+ def initialize(items)
6
+ @items = items
7
+ end
8
+
9
+ def split(groups)
10
+ return [self.items] if groups == 1
11
+ permutations = generate_list_permutations
12
+ permutation_price_differences = calculate_permutation_price_differences(permutations, groups)
13
+ sorted_price_differences = generate_list_combinations(permutation_price_differences)
14
+ list_possibilities = find_full_list(permutation_price_differences, sorted_price_differences)
15
+ output_final_lists(list_possibilities, groups)
16
+ end
17
+
18
+ private
19
+
20
+ def target_amount(divisor)
21
+ (self.items.values.reduce(:+) / divisor).round(2)
22
+ end
23
+
24
+ def generate_list_permutations
25
+ permutations = []
26
+ self.items.keys.size.times do |n|
27
+ sets = self.items.keys.combination(n+1)
28
+ sets.each do |set|
29
+ permutations << set
30
+ end
31
+ end
32
+ permutations
33
+ end
34
+
35
+ def calculate_permutation_price_differences(permutations, divisor)
36
+ permutation_price_differences = {}
37
+ permutations.each do |permutation|
38
+ total = 0.0
39
+ permutation.each_with_index do |item, n|
40
+ total += self.items[item]
41
+ end
42
+ permutation_price_differences[permutation] = (target_amount(divisor) - total).abs
43
+ end
44
+ permutation_price_differences
45
+ end
46
+
47
+ def generate_list_combinations(permutation_price_differences)
48
+ permutation_price_differences.values.sort
49
+ end
50
+
51
+ def find_full_list(permutation_price_differences, sorted_price_differences)
52
+ list = []
53
+ sorted_price_differences.each do |difference|
54
+ if sorted_price_differences.count(difference) == 1
55
+ list << permutation_price_differences.key(difference)
56
+ else
57
+ permutation_price_differences.find_all{|k,v| v == difference}.map(&:first).each do |permutation|
58
+ list << permutation
59
+ end
60
+ sorted_price_differences.delete(difference)
61
+ end
62
+ end
63
+ flattened_list = list.flatten
64
+ if self.items.keys.sort == flattened_list.uniq.sort
65
+ output = []
66
+ list.each do |sublist|
67
+ item_price = {}
68
+ sublist.each do |item|
69
+ item_price[item] = self.items[item]
70
+ end
71
+ output << item_price
72
+ end
73
+ end
74
+ output
75
+ end
76
+
77
+ def output_final_lists(lists, groups)
78
+ output = []
79
+ accounted_items = []
80
+ until output.size == groups
81
+ lists.each do |list|
82
+ if (accounted_items & list.keys).empty?
83
+ output << [list, (list.values.reduce(:+)).round(2)]
84
+ accounted_items << list.keys
85
+ accounted_items.flatten!
86
+ end
87
+ end
88
+ end
89
+ output
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,3 @@
1
+ module DivvyUp
2
+ VERSION = "0.0.1"
3
+ end
data/lib/divvy_up.rb ADDED
@@ -0,0 +1,11 @@
1
+ require "divvy_up/version"
2
+ require "divvy_up/list"
3
+
4
+ begin
5
+ require "pry"
6
+ rescue LoadError
7
+ end
8
+
9
+ module DivvyUp
10
+ # Your code goes here...
11
+ end
@@ -0,0 +1,56 @@
1
+ module DivvyUp
2
+ describe List do
3
+ let(:shopping_list) { {
4
+ orange_juice: 3,
5
+ lettuce: 7,
6
+ strawberries: 3,
7
+ eggs: 2.79,
8
+ carrots: 2.5,
9
+ onion: 1.25,
10
+ tomato: 1.25,
11
+ blueberries: 3.99,
12
+ butter: 2.69,
13
+ pasta_sauce: 2.5,
14
+ pepper: 2,
15
+ celery: 1.69
16
+ }
17
+ }
18
+
19
+ it "exists" do
20
+ expect(DivvyUp::List)
21
+ end
22
+
23
+ it "creates a new list" do
24
+ list = DivvyUp::List.new({juice: 3, apple: 1.20, chicken: 7.99})
25
+ expect(list.items).to eql({juice: 3, apple: 1.20, chicken: 7.99})
26
+ end
27
+
28
+ it "splits a list into one group" do
29
+ list = DivvyUp::List.new(shopping_list)
30
+ expect(list.split(1)).to eql([shopping_list])
31
+ end
32
+
33
+ it "splits a two-item list into two groups" do
34
+ list = DivvyUp::List.new({carrots: 2.50, celery: 2})
35
+ expect(list.split(2)).to eql([[{carrots: 2.50}, 2.50], [{celery: 2}, 2.0]])
36
+ end
37
+
38
+ it "splits a small list into two groups" do
39
+ list = DivvyUp::List.new({apples: 1.20, bananas: 2.40,
40
+ pears: 3.20, melon: 4.60})
41
+ expect(list.split(2)).to eql([[{apples: 1.20, melon: 4.60}, 5.80],
42
+ [{bananas: 2.40, pears: 3.20}, 5.60]])
43
+ end
44
+
45
+ it "split a list into three groups" do
46
+ list = DivvyUp::List.new(shopping_list)
47
+ expect(list.split(3)).to eql(
48
+ [
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]
52
+ ]
53
+ )
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,93 @@
1
+ require "divvy_up"
2
+
3
+ # This file was generated by the `rspec --init` command. Conventionally, all
4
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
5
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
6
+ # this file to always be loaded, without a need to explicitly require it in any
7
+ # files.
8
+ #
9
+ # Given that it is always loaded, you are encouraged to keep this file as
10
+ # light-weight as possible. Requiring heavyweight dependencies from this file
11
+ # will add to the boot time of your test suite on EVERY test run, even for an
12
+ # individual file that may not need all of that loaded. Instead, consider making
13
+ # a separate helper file that requires the additional dependencies and performs
14
+ # the additional setup, and require it from the spec files that actually need
15
+ # it.
16
+ #
17
+ # The `.rspec` file also contains a few flags that are not defaults but that
18
+ # users commonly want.
19
+ #
20
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
21
+ RSpec.configure do |config|
22
+ # rspec-expectations config goes here. You can use an alternate
23
+ # assertion/expectation library such as wrong or the stdlib/minitest
24
+ # assertions if you prefer.
25
+ config.expect_with :rspec do |expectations|
26
+ # This option will default to `true` in RSpec 4. It makes the `description`
27
+ # and `failure_message` of custom matchers include text for helper methods
28
+ # defined using `chain`, e.g.:
29
+ # be_bigger_than(2).and_smaller_than(4).description
30
+ # # => "be bigger than 2 and smaller than 4"
31
+ # ...rather than:
32
+ # # => "be bigger than 2"
33
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
34
+ end
35
+
36
+ # rspec-mocks config goes here. You can use an alternate test double
37
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
38
+ config.mock_with :rspec do |mocks|
39
+ # Prevents you from mocking or stubbing a method that does not exist on
40
+ # a real object. This is generally recommended, and will default to
41
+ # `true` in RSpec 4.
42
+ mocks.verify_partial_doubles = true
43
+ end
44
+
45
+ # The settings below are suggested to provide a good initial experience
46
+ # with RSpec, but feel free to customize to your heart's content.
47
+ =begin
48
+ # These two settings work together to allow you to limit a spec run
49
+ # to individual examples or groups you care about by tagging them with
50
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
51
+ # get run.
52
+ config.filter_run :focus
53
+ config.run_all_when_everything_filtered = true
54
+
55
+ # Limits the available syntax to the non-monkey patched syntax that is
56
+ # recommended. For more details, see:
57
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
58
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
59
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
60
+ config.disable_monkey_patching!
61
+
62
+ # This setting enables warnings. It's recommended, but in some cases may
63
+ # be too noisy due to issues in dependencies.
64
+ config.warnings = true
65
+
66
+ # Many RSpec users commonly either run the entire suite or an individual
67
+ # file, and it's useful to allow more verbose output when running an
68
+ # individual spec file.
69
+ if config.files_to_run.one?
70
+ # Use the documentation formatter for detailed output,
71
+ # unless a formatter has already been configured
72
+ # (e.g. via a command-line flag).
73
+ config.default_formatter = 'doc'
74
+ end
75
+
76
+ # Print the 10 slowest examples and example groups at the
77
+ # end of the spec run, to help surface which specs are running
78
+ # particularly slow.
79
+ config.profile_examples = 10
80
+
81
+ # Run specs in random order to surface order dependencies. If you find an
82
+ # order dependency and want to debug it, you can fix the order by providing
83
+ # the seed, which is printed after each run.
84
+ # --seed 1234
85
+ config.order = :random
86
+
87
+ # Seed global randomization in this process using the `--seed` CLI option.
88
+ # Setting this allows you to use `--seed` to deterministically reproduce
89
+ # test failures related to randomization by passing the same `--seed` value
90
+ # as the one that triggered the failure.
91
+ Kernel.srand config.seed
92
+ =end
93
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: divvy_up
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dave Powers
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-10 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: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
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: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Divvy up purchases to split amongst friends
70
+ email:
71
+ - djpowers89@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - divvy_up.gemspec
83
+ - lib/divvy_up.rb
84
+ - lib/divvy_up/list.rb
85
+ - lib/divvy_up/version.rb
86
+ - spec/divvy_up/list_spec.rb
87
+ - spec/spec_helper.rb
88
+ homepage: https://github.com/djpowers/divvy_up
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.4.4
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Divvy up purchases to split amongst friends
112
+ test_files:
113
+ - spec/divvy_up/list_spec.rb
114
+ - spec/spec_helper.rb