math24 1.1.3 → 2.0.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: 6471c0c4c26dce758c64a452f953efa61de09207
4
- data.tar.gz: 66c302a541221b82957288e5307dc69cb32f204c
3
+ metadata.gz: 7f74b8740c44cb687f969796f75da38eb4f8be35
4
+ data.tar.gz: 00af2b3953d8ba39dbace437aa492e98d06eb5b4
5
5
  SHA512:
6
- metadata.gz: ac6023283a671d39976afafc69cd542507c55bb7adab00428b6d10f9964188ba13e83aa9655ef5b5e902fe62ba1227be3c510a6967006bbd50ecb4e8b947c50d
7
- data.tar.gz: ce45ecf3083ad9beb4a0ccd0d551add26c07b1219373bbfefdb1cd096b8736637ce66cdcad93bb74c03e9018a2f0cc1f473e50558c295948ea5014565b3a1ff0
6
+ metadata.gz: f0b61333de0fc6abe2a4f0aeb37e7b04460b6d95badf210101c89489ff9695727dcead8ce0e47db3d6fb9481b27644a6b7ee2c005e8d037ef625d2feb943c20e
7
+ data.tar.gz: 739035936665c88a482592b1180a7659dd3d567bfc32c42aff6d71b4cce51a8f47362e8394154cdee4db45b56b87d589924fc16b437a6ac2e3519040bed0f03f
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ math24-*.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.3.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in math24.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ math24 (2.0.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.2.5)
10
+ rake (10.5.0)
11
+ rspec (3.5.0)
12
+ rspec-core (~> 3.5.0)
13
+ rspec-expectations (~> 3.5.0)
14
+ rspec-mocks (~> 3.5.0)
15
+ rspec-core (3.5.4)
16
+ rspec-support (~> 3.5.0)
17
+ rspec-expectations (3.5.0)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.5.0)
20
+ rspec-mocks (3.5.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.5.0)
23
+ rspec-support (3.5.0)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 1.13)
30
+ math24!
31
+ rake (~> 10.0)
32
+ rspec (~> 3.0)
33
+
34
+ BUNDLED WITH
35
+ 1.13.6
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Randall Reed, Jr.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # Math24
2
+ [![CircleCI](https://circleci.com/gh/randallreedjr/math24/tree/master.svg?style=svg)](https://circleci.com/gh/randallreedjr/math24/tree/master)
3
+
4
+ Math24 is a gem providing functionality to play the base version of the Math 24
5
+ game. In the base version of Math 24, a player must combine 4 integers, using
6
+ addition (+), subtraction (-), multiplication (\*), or division (/) to reach 24.
7
+ Extensions to the game included fraction, decimals, exponents, and other
8
+ mathematical functions.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'math24', '~>2.0.0'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install math24
25
+
26
+ *Note that the `< 2.0.0` version of the gem is deprecated.*
27
+
28
+ ## Usage
29
+
30
+ As of version `2.0.0`, the interactive CLI version of the game has been removed.
31
+
32
+ ### Generate a Math 24 problem
33
+
34
+ Generate a Math 24 problem. This method will return an array of four integers,
35
+ guaranteed to be a solvable Math 24 problem.
36
+
37
+ ```
38
+ Math24.generate_problem
39
+ ```
40
+
41
+ ### Check a solution
42
+
43
+ Verify a potential solution for the given problem. The problem should be passed
44
+ as an array of four integers, while the solution should be a string. This
45
+ method will return true if the solution is valid, and false otherwise.
46
+
47
+ ```
48
+ Math24.check(problem, solution)
49
+ ```
50
+
51
+ ### Solve a problem
52
+
53
+ Solve a given Math 24 problem. The problem should be passed as an array of four
54
+ integers. This method will return an expression string containing the same four
55
+ integers from the given problem that, when evaluated, will equal 24.
56
+
57
+ ```
58
+ Math24.solve(problem)
59
+ ```
60
+
61
+ ## Development
62
+
63
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
64
+
65
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
66
+
67
+ ## Contributing
68
+
69
+ Bug reports and pull requests are welcome on GitHub at https://github.com/randallreedjr/math24.
70
+
71
+ ## License
72
+
73
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "math24"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ module Math24
2
+ VERSION = "2.0.0"
3
+ end
data/lib/math24.rb CHANGED
@@ -1,35 +1,66 @@
1
- require 'math24solver'
1
+ require "math24/version"
2
2
 
3
- class Math24
3
+ module Math24
4
+ OPERATORS = ['+','-','*','/']
5
+ def self.generate_problem
6
+ loop do
7
+ problem = Array.new(4) { rand(1...9) }
8
+ return problem if solve(problem)
9
+ end
10
+ end
4
11
 
5
- attr_accessor :numbers
6
- attr_reader :last_answer
12
+ def self.solve(problem)
13
+ raise ArgumentError unless /\A\d{4}\z/.match problem.join
7
14
 
8
- def initialize
9
- end
15
+ op_permutation = OPERATORS.repeated_permutation(3)
16
+ # Unique permutations removes duplicates when a number appears multiple
17
+ # times in the problem
18
+ num_permutation = problem.permutation(4).to_a.uniq
19
+
20
+ num_permutation.each do |numbers|
21
+ op_permutation.each do |operators|
22
+ begin
23
+ forward_result = instance_eval("((#{numbers[0].to_f} #{operators[0]} #{numbers[1]}.to_f) #{operators[1]} #{numbers[2]}.to_f) #{operators[2]} #{numbers[3].to_f}")
24
+ rescue ZeroDivisionError
25
+ forward_result = 0
26
+ end
27
+
28
+ begin
29
+ alternate_result = instance_eval("(#{numbers[0].to_f} #{operators[0]} #{numbers[1].to_f}) #{operators[1]} (#{numbers[2].to_f} #{operators[2]} #{numbers[3].to_f})")
30
+ rescue ZeroDivisionError
31
+ alternate_result = 0
32
+ end
33
+
34
+ begin
35
+ reverse_result = instance_eval("#{numbers[0].to_f} #{operators[0]} (#{numbers[1].to_f} #{operators[1]} (#{numbers[2].to_f} #{operators[2]} #{numbers[3].to_f}))")
36
+ rescue ZeroDivisionError
37
+ reverse_result = 0
38
+ end
10
39
 
11
- def solution?(solution)
12
- if solution.scan(/\A(\(*(\d{1}[()\s]*[*+-\/]+[()\s]*){3}\d{1}\)*)\z/).flatten.first == solution
13
- @last_answer = instance_eval(solution)
14
- return last_answer == 24
15
- else
16
- @last_answer = "???"
40
+ if forward_result == 24
41
+ if (operators.include?("+") || operators.include?("-")) && (operators.include?("*") || operators.include?("/"))
42
+ #Might need parentheses for order of operations
43
+ return "((#{numbers[0]} #{operators[0]} #{numbers[1]}) #{operators[1]} #{numbers[2]}) #{operators[2]} #{numbers[3]}"
44
+ else
45
+ return "#{numbers[0]} #{operators[0]} #{numbers[1]} #{operators[1]} #{numbers[2]} #{operators[2]} #{numbers[3]}"
46
+ end
47
+ elsif alternate_result == 24
48
+ return "(#{numbers[0]} #{operators[0]} #{numbers[1]}) #{operators[1]} (#{numbers[2]} #{operators[2]} #{numbers[3]})"
49
+ elsif reverse_result == 24
50
+ return "#{numbers[0]} #{operators[0]} (#{numbers[1]} #{operators[1]} (#{numbers[2]} #{operators[2]} #{numbers[3]}))"
51
+ end
52
+ end
17
53
  end
54
+
18
55
  return false
19
56
  end
20
57
 
21
- def generate_problem()
22
- loop do
23
- numbers = []
24
- 4.times do |i|
25
- numbers << rand(1..9).to_s
26
- end
27
- math24 = Math24Solver.new()
28
- math24.numbers = numbers
29
- if math24.solve() != "No solution found"
30
- @numbers = numbers
31
- return numbers
32
- end
58
+ def self.check(problem, solution)
59
+ raise ArgumentError unless /\A\d{4}\z/.match problem.join
60
+ raise ArgumentError unless solution.is_a? String
61
+ raise ArgumentError unless /\A(\(*(\d{1}[()\s]*[*+-\/]+[()\s]*){3}\d{1}\)*)\z/.match(solution)
62
+
63
+ problem.count {|i| solution.include?(i.to_s) } == 4 &&
64
+ instance_eval(solution) == 24
33
65
  end
34
- end
35
- end
66
+ end
data/math24.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'math24/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "math24"
8
+ spec.version = Math24::VERSION
9
+ spec.authors = ["Randall Reed, Jr."]
10
+ spec.email = ["randallreedjr@gmail.com"]
11
+
12
+ spec.summary = %q{Gem to provide Math 24 game functionality.}
13
+ spec.description = %q{Randomly generates a solvable 24 Game problem, verifies user's solution, or finds a solution for a given set of numbers.}
14
+ spec.homepage = "http://www.getto24.com"
15
+ spec.license = "MIT"
16
+
17
+
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
20
+ f.match(%r{^(test|spec|features)/})
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.13"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ end
metadata CHANGED
@@ -1,27 +1,79 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: math24
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.3
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Randall Reed, Jr.
8
8
  autorequire:
9
- bindir: bin
9
+ bindir: exe
10
10
  cert_chain: []
11
- date: 2014-07-08 00:00:00.000000000 Z
12
- dependencies: []
13
- description: Randomly generates a solvable 24 Game problem, verifies user's solution
14
- email: randallreedjr@gmail.com
15
- executables:
16
- - math24
11
+ date: 2016-12-22 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.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
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: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Randomly generates a solvable 24 Game problem, verifies user's solution,
56
+ or finds a solution for a given set of numbers.
57
+ email:
58
+ - randallreedjr@gmail.com
59
+ executables: []
17
60
  extensions: []
18
61
  extra_rdoc_files: []
19
62
  files:
20
- - bin/math24
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".ruby-version"
66
+ - Gemfile
67
+ - Gemfile.lock
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
21
73
  - lib/math24.rb
22
- - lib/math24gamecli.rb
23
- - lib/math24solver.rb
24
- homepage: http://rubygems.org/gems/math24
74
+ - lib/math24/version.rb
75
+ - math24.gemspec
76
+ homepage: http://www.getto24.com
25
77
  licenses:
26
78
  - MIT
27
79
  metadata: {}
@@ -41,8 +93,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
41
93
  version: '0'
42
94
  requirements: []
43
95
  rubyforge_project:
44
- rubygems_version: 2.2.2
96
+ rubygems_version: 2.5.2
45
97
  signing_key:
46
98
  specification_version: 4
47
- summary: Command-line Math 24 game
99
+ summary: Gem to provide Math 24 game functionality.
48
100
  test_files: []
data/bin/math24 DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'math24'
4
- require_relative '../lib/math24solver.rb'
5
- require_relative '../lib/math24gamecli.rb'
6
-
7
- cli = Math24GameCli.new
8
- cli.run()
data/lib/math24gamecli.rb DELETED
@@ -1,20 +0,0 @@
1
- require 'math24'
2
- class Math24GameCli
3
- def run()
4
- game = Math24.new()
5
- problem = game.generate_problem().join(' ')
6
-
7
- loop do
8
- puts "Solve: #{problem}"
9
- attempt = gets.chomp
10
- break if attempt.downcase == "exit"
11
-
12
- if game.solution?(attempt)
13
- puts "Correct!\n\n"
14
- problem = game.generate_problem().join(' ')
15
- else
16
- puts "Try again...\n\n" unless problem.empty?
17
- end
18
- end
19
- end
20
- end
data/lib/math24solver.rb DELETED
@@ -1,68 +0,0 @@
1
- class Math24Solver
2
- attr_accessor :numbers
3
- attr_reader :operators
4
- attr_accessor :target
5
-
6
- def initialize(operators = ["+", "-", "*", "/"])
7
- @operators = operators
8
- @target = 24
9
- end
10
-
11
- def solve(problem_numbers = [])
12
- if problem_numbers.empty?
13
- return "Please provide numbers" if @numbers.empty?
14
- else
15
- @numbers = problem_numbers
16
- end
17
-
18
- @numbers.each do |number|
19
- if (number.to_i < 1) || (number.to_i > 9) || (number.to_f != number.to_i)
20
- return "Please only enter integers between 1 and 9"
21
- end
22
- end
23
-
24
- op_permutation = @operators.repeated_permutation(3)
25
- num_permutation = @numbers.permutation(4).to_a.uniq
26
-
27
- num_permutation.each do |numbers|
28
- op_permutation.each do |operators|
29
- begin
30
- forward_result = instance_eval("((#{numbers[0].to_f} #{operators[0]} #{numbers[1]}.to_f) #{operators[1]} #{numbers[2]}.to_f) #{operators[2]} #{numbers[3].to_f}")
31
- rescue ZeroDivisionError
32
- forward_result = 0
33
- end
34
- begin
35
- alternate_result = instance_eval("(#{numbers[0].to_f} #{operators[0]} #{numbers[1].to_f}) #{operators[1]} (#{numbers[2].to_f} #{operators[2]} #{numbers[3].to_f})")
36
- rescue ZeroDivisionError
37
- alternate_result = 0
38
- end
39
-
40
- begin
41
- reverse_result = instance_eval("#{numbers[0].to_f} #{operators[0]} (#{numbers[1].to_f} #{operators[1]} (#{numbers[2].to_f} #{operators[2]} #{numbers[3].to_f}))")
42
- rescue ZeroDivisionError
43
- reverse_result = 0
44
- end
45
-
46
- if forward_result == @target
47
- if (operators.include?("+") || operators.include?("-")) && (operators.include?("*") || operators.include?("/"))
48
- #Might need parentheses for order of operations
49
- return "((#{numbers[0]} #{operators[0]} #{numbers[1]}) #{operators[1]} #{numbers[2]}) #{operators[2]} #{numbers[3]} = 24"
50
- else
51
- return "#{numbers[0]} #{operators[0]} #{numbers[1]} #{operators[1]} #{numbers[2]} #{operators[2]} #{numbers[3]} = 24"
52
- end
53
- elsif alternate_result == @target
54
- return "(#{numbers[0]} #{operators[0]} #{numbers[1]}) #{operators[1]} (#{numbers[2]} #{operators[2]} #{numbers[3]}) = 24"
55
- elsif reverse_result == @target
56
- return "#{numbers[0]} #{operators[0]} (#{numbers[1]} #{operators[1]} (#{numbers[2]} #{operators[2]} #{numbers[3]})) = 24"
57
- end
58
- end
59
- end
60
- return "No solution found"
61
- end
62
- end
63
-
64
-
65
-
66
-
67
-
68
-