math24 2.0.0 → 2.0.1
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 +4 -4
- data/.gitignore +1 -0
- data/README.md +1 -0
- data/lib/math24.rb +26 -12
- data/lib/math24/version.rb +1 -1
- data/math24.gemspec +1 -0
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 857a194911be2e38f064f35f9721244adb7b3f80
|
4
|
+
data.tar.gz: 324ffd0100d3904ac2f282cf7d81b911b8cf3620
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f03d431b6b5e5f2197c01fce69f11bd3328ff400e34c2f988a9aecf249926b61b28dfe54f8ebc5d6bec362816eecdefe3325ce9c99812c2c8c2fa72235e26b4f
|
7
|
+
data.tar.gz: e47fe0aa504d1c30c8125cdb42f0be3615f46abc136a3d39a8b63f795458d2cbb1e6ed39203d88af30cf10ba926709af5410fbae35ff6be0bc8b7a75e0e8eb72
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
# Math24
|
2
2
|
[](https://circleci.com/gh/randallreedjr/math24/tree/master)
|
3
|
+
[](https://badge.fury.io/rb/math24)
|
3
4
|
|
4
5
|
Math24 is a gem providing functionality to play the base version of the Math 24
|
5
6
|
game. In the base version of Math 24, a player must combine 4 integers, using
|
data/lib/math24.rb
CHANGED
@@ -20,33 +20,36 @@ module Math24
|
|
20
20
|
num_permutation.each do |numbers|
|
21
21
|
op_permutation.each do |operators|
|
22
22
|
begin
|
23
|
-
|
23
|
+
proposed_solution = "((#{numbers[0].to_f} #{operators[0]} #{numbers[1]}.to_f) #{operators[1]} #{numbers[2]}.to_f) #{operators[2]} #{numbers[3].to_f}"
|
24
|
+
forward_result = instance_eval(proposed_solution)
|
24
25
|
rescue ZeroDivisionError
|
25
26
|
forward_result = 0
|
26
27
|
end
|
27
28
|
|
28
29
|
begin
|
29
|
-
|
30
|
+
proposed_solution = "(#{numbers[0].to_f} #{operators[0]} #{numbers[1].to_f}) #{operators[1]} (#{numbers[2].to_f} #{operators[2]} #{numbers[3].to_f})"
|
31
|
+
alternate_result = instance_eval(proposed_solution)
|
30
32
|
rescue ZeroDivisionError
|
31
33
|
alternate_result = 0
|
32
34
|
end
|
33
35
|
|
34
36
|
begin
|
35
|
-
|
37
|
+
proposed_solution = "#{numbers[0].to_f} #{operators[0]} (#{numbers[1].to_f} #{operators[1]} (#{numbers[2].to_f} #{operators[2]} #{numbers[3].to_f}))"
|
38
|
+
reverse_result = instance_eval(proposed_solution)
|
36
39
|
rescue ZeroDivisionError
|
37
40
|
reverse_result = 0
|
38
41
|
end
|
39
42
|
|
40
|
-
if forward_result == 24
|
43
|
+
if forward_result == 24 && integer_division_only?(proposed_solution)
|
41
44
|
if (operators.include?("+") || operators.include?("-")) && (operators.include?("*") || operators.include?("/"))
|
42
45
|
#Might need parentheses for order of operations
|
43
46
|
return "((#{numbers[0]} #{operators[0]} #{numbers[1]}) #{operators[1]} #{numbers[2]}) #{operators[2]} #{numbers[3]}"
|
44
47
|
else
|
45
48
|
return "#{numbers[0]} #{operators[0]} #{numbers[1]} #{operators[1]} #{numbers[2]} #{operators[2]} #{numbers[3]}"
|
46
49
|
end
|
47
|
-
elsif alternate_result == 24
|
50
|
+
elsif alternate_result == 24 && integer_division_only?(proposed_solution)
|
48
51
|
return "(#{numbers[0]} #{operators[0]} #{numbers[1]}) #{operators[1]} (#{numbers[2]} #{operators[2]} #{numbers[3]})"
|
49
|
-
elsif reverse_result == 24
|
52
|
+
elsif reverse_result == 24 && integer_division_only?(proposed_solution)
|
50
53
|
return "#{numbers[0]} #{operators[0]} (#{numbers[1]} #{operators[1]} (#{numbers[2]} #{operators[2]} #{numbers[3]}))"
|
51
54
|
end
|
52
55
|
end
|
@@ -55,12 +58,23 @@ module Math24
|
|
55
58
|
return false
|
56
59
|
end
|
57
60
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
61
|
+
def self.check(problem, solution)
|
62
|
+
raise ArgumentError unless /\A\d{4}\z/.match problem.join
|
63
|
+
raise ArgumentError unless solution.is_a? String
|
64
|
+
raise ArgumentError unless /\A(\(*(\d{1}[()\s]*[*+-\/]+[()\s]*){3}\d{1}\)*)\z/.match(solution)
|
65
|
+
|
66
|
+
problem.count {|i| solution.include?(i.to_s) } == 4 &&
|
67
|
+
instance_eval(solution) == 24
|
68
|
+
end
|
62
69
|
|
63
|
-
|
64
|
-
|
70
|
+
private
|
71
|
+
|
72
|
+
def self.integer_division_only? proposed_solution
|
73
|
+
# float and integer division yield same result
|
74
|
+
begin
|
75
|
+
instance_eval(proposed_solution) == instance_eval(proposed_solution.delete('.0'))
|
76
|
+
rescue ZeroDivisionError
|
77
|
+
false
|
65
78
|
end
|
79
|
+
end
|
66
80
|
end
|
data/lib/math24/version.rb
CHANGED
data/math24.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: math24
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Randall Reed, Jr.
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-01-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: byebug
|
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'
|
55
69
|
description: Randomly generates a solvable 24 Game problem, verifies user's solution,
|
56
70
|
or finds a solution for a given set of numbers.
|
57
71
|
email:
|