equation_builder 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source 'http://rubygems.org'
2
+
3
+ group :development do
4
+ gem 'rake'
5
+ gem 'bundler'
6
+ gem 'jeweler'
7
+ end
8
+
9
+ group :test do
10
+ gem 'rspec'
11
+ gem 'simplecov'
12
+ gem 'autotest'
13
+ end
@@ -0,0 +1,41 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ ZenTest (4.6.2)
5
+ autotest (4.4.6)
6
+ ZenTest (>= 4.4.1)
7
+ diff-lcs (1.1.3)
8
+ git (1.2.5)
9
+ jeweler (1.8.3)
10
+ bundler (~> 1.0)
11
+ git (>= 1.2.5)
12
+ rake
13
+ rdoc
14
+ json (1.6.5)
15
+ multi_json (1.0.4)
16
+ rake (0.9.2.2)
17
+ rdoc (3.12)
18
+ json (~> 1.4)
19
+ rspec (2.8.0)
20
+ rspec-core (~> 2.8.0)
21
+ rspec-expectations (~> 2.8.0)
22
+ rspec-mocks (~> 2.8.0)
23
+ rspec-core (2.8.0)
24
+ rspec-expectations (2.8.0)
25
+ diff-lcs (~> 1.1.2)
26
+ rspec-mocks (2.8.0)
27
+ simplecov (0.5.4)
28
+ multi_json (~> 1.0.3)
29
+ simplecov-html (~> 0.5.3)
30
+ simplecov-html (0.5.3)
31
+
32
+ PLATFORMS
33
+ ruby
34
+
35
+ DEPENDENCIES
36
+ autotest
37
+ bundler
38
+ jeweler
39
+ rake
40
+ rspec
41
+ simplecov
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Alexander Shestakov
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,19 @@
1
+ = equation_builder
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to equation_builder
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2012 Alexander Shestakov. See LICENSE.txt for
18
+ further details.
19
+
@@ -0,0 +1,36 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development, :test)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ gem.name = "equation_builder"
17
+ gem.homepage = "http://github.com/maksar/equation_builder"
18
+ gem.license = "MIT"
19
+ gem.summary = %Q{Gem to build an equation from sequence of numbers and arithmetic operations, evaluation result of which will equal to specified number.}
20
+ gem.description = %Q{By given list of numbers [1, 3, 4, 6] and list of available operators ['/', '-'] both with desired result (24), this gem will build an equasion 6/(1-3/4)}
21
+ gem.email = "Maksar.mail@gmail.com"
22
+ gem.authors = ["Alexander Shestakov"]
23
+
24
+ gem.add_development_dependency 'rspec'
25
+ gem.add_development_dependency 'jeweler'
26
+ gem.add_development_dependency 'simplecov'
27
+ gem.add_development_dependency 'bundler'
28
+ gem.add_development_dependency 'autotest'
29
+ end
30
+ Jeweler::RubygemsDotOrgTasks.new
31
+
32
+ require 'rspec/core/rake_task'
33
+ RSpec::Core::RakeTask.new(:spec) do |spec|
34
+ spec.pattern = FileList['spec/**/*_spec.rb']
35
+ end
36
+ task :default => :spec
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,71 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "equation_builder"
8
+ s.version = "0.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Alexander Shestakov"]
12
+ s.date = "2012-02-19"
13
+ s.description = "By given list of numbers [1, 3, 4, 6] and list of available operators ['/', '-'] both with desired result (24), this gem will build an equasion 6/(1-3/4)"
14
+ s.email = "Maksar.mail@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".travis.yml",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE.txt",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "equation_builder.gemspec",
28
+ "lib/equation_builder.rb",
29
+ "spec/equation_builder_spec.rb",
30
+ "spec/spec_helper.rb"
31
+ ]
32
+ s.homepage = "http://github.com/maksar/equation_builder"
33
+ s.licenses = ["MIT"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = "1.8.10"
36
+ s.summary = "Gem to build an equation from sequence of numbers and arithmetic operations, evaluation result of which will equal to specified number."
37
+
38
+ if s.respond_to? :specification_version then
39
+ s.specification_version = 3
40
+
41
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
42
+ s.add_development_dependency(%q<rake>, [">= 0"])
43
+ s.add_development_dependency(%q<bundler>, [">= 0"])
44
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
45
+ s.add_development_dependency(%q<rspec>, [">= 0"])
46
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
47
+ s.add_development_dependency(%q<simplecov>, [">= 0"])
48
+ s.add_development_dependency(%q<bundler>, [">= 0"])
49
+ s.add_development_dependency(%q<autotest>, [">= 0"])
50
+ else
51
+ s.add_dependency(%q<rake>, [">= 0"])
52
+ s.add_dependency(%q<bundler>, [">= 0"])
53
+ s.add_dependency(%q<jeweler>, [">= 0"])
54
+ s.add_dependency(%q<rspec>, [">= 0"])
55
+ s.add_dependency(%q<jeweler>, [">= 0"])
56
+ s.add_dependency(%q<simplecov>, [">= 0"])
57
+ s.add_dependency(%q<bundler>, [">= 0"])
58
+ s.add_dependency(%q<autotest>, [">= 0"])
59
+ end
60
+ else
61
+ s.add_dependency(%q<rake>, [">= 0"])
62
+ s.add_dependency(%q<bundler>, [">= 0"])
63
+ s.add_dependency(%q<jeweler>, [">= 0"])
64
+ s.add_dependency(%q<rspec>, [">= 0"])
65
+ s.add_dependency(%q<jeweler>, [">= 0"])
66
+ s.add_dependency(%q<simplecov>, [">= 0"])
67
+ s.add_dependency(%q<bundler>, [">= 0"])
68
+ s.add_dependency(%q<autotest>, [">= 0"])
69
+ end
70
+ end
71
+
@@ -0,0 +1,65 @@
1
+ class EquationBuilder
2
+
3
+ def initialize _numbers, _result, _operators
4
+ @numbers = _numbers
5
+ @result = _result
6
+ @operators = Array(_operators)
7
+
8
+ @matcher = proc { |numbers, operators, result| numbers.zip(operators).flatten.join if eval(make_float(numbers).zip(operators).flatten.join) == result }
9
+ end
10
+
11
+
12
+ def solve
13
+ solve_simple || solve_with_parentheses || nil
14
+ end
15
+
16
+ private
17
+
18
+ def solve_simple
19
+ with_permutations do |numbers, operators|
20
+ result = @matcher.call(numbers, operators, @result)
21
+ return result if result
22
+ end
23
+ nil
24
+ end
25
+
26
+ def solve_with_parentheses
27
+ with_permutations do |numbers, operators|
28
+ with_parentheses(@numbers.length.div(2), numbers, (0..@numbers.length).to_a) do |numbers_with_parentheses|
29
+ result = @matcher.call(numbers_with_parentheses, operators, @result)
30
+ return result if result
31
+ end
32
+ end
33
+ nil
34
+ end
35
+
36
+ def with_parentheses number, numbers, parentheses_options, &block
37
+ return if number == 0
38
+
39
+ parentheses_options.combination(2) do |positions|
40
+ block.call add_parentheses numbers.dup, positions
41
+ end
42
+
43
+ parentheses_options.combination(2) do |positions|
44
+ with_parentheses number - 1, add_parentheses(numbers.dup, positions), parentheses_options, &block
45
+ end
46
+ end
47
+
48
+ def with_permutations &block
49
+ @numbers.permutation do |numbers|
50
+ @operators.repeated_permutation @numbers.length - 1 do |operators|
51
+ block.call(numbers, operators)
52
+ end
53
+ end
54
+ end
55
+
56
+ def make_float numbers
57
+ numbers.map { |element| "#{element}*1.0" }
58
+ end
59
+
60
+ def add_parentheses numbers, position
61
+ numbers[position.first] = "(#{numbers[position.first]}"
62
+ numbers[position.last] = "#{numbers[position.last]})"
63
+ numbers
64
+ end
65
+ end
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+
3
+ require 'equation_builder'
4
+
5
+ describe EquationBuilder do
6
+ context 'simple equations' do
7
+ context 'with + operator' do
8
+ subject { EquationBuilder.new([2, 4], 6, '+') }
9
+ its(:solve) { should == '2+4' }
10
+ end
11
+
12
+ context 'with - operator' do
13
+ subject { EquationBuilder.new([2, 4], -2, '-') }
14
+ its(:solve) { should == '2-4' }
15
+ end
16
+
17
+ context 'with multiple operators' do
18
+ subject { EquationBuilder.new([2, 4, 6], 4, %w(- +)) }
19
+ its(:solve) { should == '2-4+6' }
20
+ end
21
+
22
+ context 'with repeating operator' do
23
+ subject { EquationBuilder.new([2, 4, 6], 12, '+') }
24
+ its(:solve) { should == '2+4+6' }
25
+ end
26
+
27
+ context 'with multiple repeating operators' do
28
+ subject { EquationBuilder.new([2, 4, 6, 8], 0, %w(+ -)) }
29
+ its(:solve) { should == '2-4-6+8' }
30
+ end
31
+ end
32
+
33
+ context 'with long equation' do
34
+ subject { EquationBuilder.new([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 0, %w(* -)) }
35
+ its(:solve) { should == '1*2*3*4*5-6*9-7*8-10' }
36
+ end
37
+
38
+ context 'with float numbers' do
39
+ subject { EquationBuilder.new([1, 2], 0.5, %w(/ *)) }
40
+ its(:solve) { should == '1/2' }
41
+ end
42
+
43
+ context 'with parentheses' do
44
+ context 'with single parentheses' do
45
+ subject { EquationBuilder.new([1, 2, 4, 5], 17, %w(+ *)) }
46
+ its(:solve) { should == '(1+2)*4+5' }
47
+ end
48
+
49
+ context 'with double parentheses' do
50
+ subject { EquationBuilder.new([5, 5, 5, 5], 100, %w(+ *)) }
51
+ its(:solve) { should == '(5+5)*(5+5)' }
52
+ end
53
+ end
54
+
55
+ context 'ultimate example' do
56
+ subject { EquationBuilder.new([1, 3, 4, 6], 24, %w(- /)) }
57
+ its(:solve) { should == '6/(1-3/4)' }
58
+ end
59
+
60
+ context 'equation not found' do
61
+ subject { EquationBuilder.new([1, 2], 4, '+') }
62
+ its(:solve) { should be_nil }
63
+ end
64
+ end
@@ -0,0 +1,4 @@
1
+ require 'simplecov'
2
+ SimpleCov.start do
3
+ add_filter 'spec/'
4
+ end
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: equation_builder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alexander Shestakov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: &70120347953300 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70120347953300
25
+ - !ruby/object:Gem::Dependency
26
+ name: bundler
27
+ requirement: &70120347952120 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70120347952120
36
+ - !ruby/object:Gem::Dependency
37
+ name: jeweler
38
+ requirement: &70120347951500 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70120347951500
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: &70120347967040 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70120347967040
58
+ - !ruby/object:Gem::Dependency
59
+ name: jeweler
60
+ requirement: &70120347965600 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70120347965600
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: &70120347964180 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *70120347964180
80
+ - !ruby/object:Gem::Dependency
81
+ name: bundler
82
+ requirement: &70120347962580 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *70120347962580
91
+ - !ruby/object:Gem::Dependency
92
+ name: autotest
93
+ requirement: &70120347961520 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ type: :development
100
+ prerelease: false
101
+ version_requirements: *70120347961520
102
+ description: By given list of numbers [1, 3, 4, 6] and list of available operators
103
+ ['/', '-'] both with desired result (24), this gem will build an equasion 6/(1-3/4)
104
+ email: Maksar.mail@gmail.com
105
+ executables: []
106
+ extensions: []
107
+ extra_rdoc_files:
108
+ - LICENSE.txt
109
+ - README.rdoc
110
+ files:
111
+ - .travis.yml
112
+ - Gemfile
113
+ - Gemfile.lock
114
+ - LICENSE.txt
115
+ - README.rdoc
116
+ - Rakefile
117
+ - VERSION
118
+ - equation_builder.gemspec
119
+ - lib/equation_builder.rb
120
+ - spec/equation_builder_spec.rb
121
+ - spec/spec_helper.rb
122
+ homepage: http://github.com/maksar/equation_builder
123
+ licenses:
124
+ - MIT
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - ! '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ segments:
136
+ - 0
137
+ hash: -2686214661129260008
138
+ required_rubygems_version: !ruby/object:Gem::Requirement
139
+ none: false
140
+ requirements:
141
+ - - ! '>='
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ requirements: []
145
+ rubyforge_project:
146
+ rubygems_version: 1.8.10
147
+ signing_key:
148
+ specification_version: 3
149
+ summary: Gem to build an equation from sequence of numbers and arithmetic operations,
150
+ evaluation result of which will equal to specified number.
151
+ test_files: []