pairs 0.1.0

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: 15663f641b3dd19d88544bcb8f2a76c5ba71a954
4
+ data.tar.gz: 06245a8493128bc129b55163dbbfcd7014d43f4a
5
+ SHA512:
6
+ metadata.gz: 7349ecdb60cb5518f634bf525c781fdbd7a968f4526e99ad3917c722055fdb0fb77bf33dda9f866703d8204e82be074a9ee5fdb2370827e5147385b4bb152e21
7
+ data.tar.gz: e91e1664c55340d8e3cabcc92b8cfce40b987dc34945b3d3f63e8da1749df7cec7b3bb4ba08c44f83bd2e7e764e8b275d090d0db1b7779a57636dcaed0da8a48
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --warnings
3
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # Changelog
2
+
3
+ ## 0.1.0
4
+
5
+ * Initial release, basic constraints
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Justin Campbell
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,29 @@
1
+ # Pairs
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'pairs'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install pairs
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/pairs/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'generative/rake_task'
3
+ require 'rspec/core/rake_task'
4
+
5
+ task default: [:spec]
6
+
7
+ RSpec::Core::RakeTask.new
8
+ Generative::RakeTask.new
data/lib/pairs.rb ADDED
@@ -0,0 +1,64 @@
1
+ class Pairs
2
+ attr_reader :max_attempts, :block
3
+
4
+ def initialize(max_attempts: 1000, &block)
5
+ @max_attempts = max_attempts
6
+ @block = block
7
+ end
8
+
9
+ def solve!
10
+ clean_room.instance_exec(&block)
11
+
12
+ max_attempts.times do
13
+ all = clean_room.__items__.values.flatten
14
+ result = []
15
+
16
+ until all.empty?
17
+ these = all.sample(2)
18
+
19
+ next unless clean_room.__constraints__.all? { |constraint|
20
+ constraint.call(these)
21
+ }
22
+
23
+ result << these
24
+
25
+ these.each do |item|
26
+ all.delete item
27
+ end
28
+ end
29
+
30
+ return result
31
+ end
32
+ end
33
+
34
+ def solution
35
+ @solution ||= solve!
36
+ end
37
+
38
+ private
39
+
40
+ def clean_room
41
+ @clean_room ||= CleanRoom.new
42
+ end
43
+
44
+ class CleanRoom
45
+ def __constraints__
46
+ @__constraints__ ||= []
47
+ end
48
+
49
+ def __items__
50
+ @__items__ ||= {}
51
+ end
52
+
53
+ def constraint(&block)
54
+ __constraints__ << block
55
+ end
56
+
57
+ def method_missing(method_name, *args, &block)
58
+ return super unless args.count == 1
59
+
60
+ __items__[method_name] ||= []
61
+ __items__[method_name] << args.first
62
+ end
63
+ end
64
+ end
data/pairs.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "pairs"
7
+ spec.version = "0.1.0"
8
+ spec.authors = ["Justin Campbell"]
9
+ spec.email = ["justin@justincampbell.me"]
10
+ spec.summary = "Constraint solver for pairs"
11
+ spec.description = "Constraint solver for pairs"
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.6"
21
+ spec.add_development_dependency "generative"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,63 @@
1
+ require 'spec_helper'
2
+
3
+ describe Pairs do
4
+ let(:pairs) { Pairs.new(&block) }
5
+ let(:block) { -> { } }
6
+ let(:solution) { pairs.solution }
7
+
8
+ context "without a block" do
9
+ it "returns an empty solution" do
10
+ expect(solution).to eq([])
11
+ end
12
+ end
13
+
14
+ context "with 1 item" do
15
+ let(:block) { -> { item "a" } }
16
+
17
+ it "returns that item" do
18
+ expect(solution).to eq([["a"]])
19
+ end
20
+ end
21
+
22
+ context "with 2 items" do
23
+ let(:block) { -> { item "a"; item "b" } }
24
+
25
+ generative do
26
+ it "returns both items" do
27
+ expect(solution.flatten).to match_array(%w[a b])
28
+ end
29
+ end
30
+ end
31
+
32
+ context "with 3 items" do
33
+ let(:block) { -> { item "a"; item "b"; item "c" } }
34
+
35
+ generative do
36
+ it "has all items" do
37
+ expect(solution.flatten).to match_array(%w[a b c])
38
+ end
39
+
40
+ it "groups them in pairs" do
41
+ expect(solution.first.size).to eq(2)
42
+ end
43
+ end
44
+ end
45
+
46
+ context "with a constraint" do
47
+ context "with only one solution" do
48
+ let(:block) {
49
+ -> {
50
+ n 1; n 2; n 3
51
+ constraint { |both| both.reduce(:+) == 3 }
52
+ }
53
+ }
54
+
55
+ generative do
56
+ it "returns the solution" do
57
+ expect(solution.first).to match_array([1, 2])
58
+ expect(solution.last).to match_array([3])
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,81 @@
1
+ require 'generative'
2
+ require 'pairs'
3
+
4
+ # This file was generated by the `rspec --init` command. Conventionally, all
5
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
6
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
7
+ # file to always be loaded, without a need to explicitly require it in any 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, make a
13
+ # separate helper file that requires this one and then use it only in the specs
14
+ # that actually need it.
15
+ #
16
+ # The `.rspec` file also contains a few flags that are not defaults but that
17
+ # users commonly want.
18
+ #
19
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
20
+ RSpec.configure do |config|
21
+ # The settings below are suggested to provide a good initial experience
22
+ # with RSpec, but feel free to customize to your heart's content.
23
+ =begin
24
+ # These two settings work together to allow you to limit a spec run
25
+ # to individual examples or groups you care about by tagging them with
26
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
27
+ # get run.
28
+ config.filter_run :focus
29
+ config.run_all_when_everything_filtered = true
30
+
31
+ # Many RSpec users commonly either run the entire suite or an individual
32
+ # file, and it's useful to allow more verbose output when running an
33
+ # individual spec file.
34
+ if config.files_to_run.one?
35
+ # Use the documentation formatter for detailed output,
36
+ # unless a formatter has already been configured
37
+ # (e.g. via a command-line flag).
38
+ config.default_formatter = 'doc'
39
+ end
40
+
41
+ # Print the 10 slowest examples and example groups at the
42
+ # end of the spec run, to help surface which specs are running
43
+ # particularly slow.
44
+ config.profile_examples = 10
45
+
46
+ # Run specs in random order to surface order dependencies. If you find an
47
+ # order dependency and want to debug it, you can fix the order by providing
48
+ # the seed, which is printed after each run.
49
+ # --seed 1234
50
+ config.order = :random
51
+
52
+ # Seed global randomization in this process using the `--seed` CLI option.
53
+ # Setting this allows you to use `--seed` to deterministically reproduce
54
+ # test failures related to randomization by passing the same `--seed` value
55
+ # as the one that triggered the failure.
56
+ Kernel.srand config.seed
57
+
58
+ # rspec-expectations config goes here. You can use an alternate
59
+ # assertion/expectation library such as wrong or the stdlib/minitest
60
+ # assertions if you prefer.
61
+ config.expect_with :rspec do |expectations|
62
+ # Enable only the newer, non-monkey-patching expect syntax.
63
+ # For more details, see:
64
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
65
+ expectations.syntax = :expect
66
+ end
67
+
68
+ # rspec-mocks config goes here. You can use an alternate test double
69
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
70
+ config.mock_with :rspec do |mocks|
71
+ # Enable only the newer, non-monkey-patching expect syntax.
72
+ # For more details, see:
73
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
74
+ mocks.syntax = :expect
75
+
76
+ # Prevents you from mocking or stubbing a method that does not exist on
77
+ # a real object. This is generally recommended.
78
+ mocks.verify_partial_doubles = true
79
+ end
80
+ =end
81
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pairs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Justin Campbell
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-13 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.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: generative
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
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: rspec
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: Constraint solver for pairs
70
+ email:
71
+ - justin@justincampbell.me
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .rspec
78
+ - CHANGELOG.md
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - lib/pairs.rb
84
+ - pairs.gemspec
85
+ - spec/lib/pairs_spec.rb
86
+ - spec/spec_helper.rb
87
+ homepage: ''
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.0.14
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Constraint solver for pairs
111
+ test_files:
112
+ - spec/lib/pairs_spec.rb
113
+ - spec/spec_helper.rb
114
+ has_rdoc: