hypatia 0.0.2
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 +7 -0
- data/lib/hypatia/difficulty-calculator.rb +196 -0
- data/lib/hypatia/formula.rb +54 -0
- metadata +89 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 91c59c1282b89915ba227ca03a0f5fc355a39470
|
4
|
+
data.tar.gz: 8d7f3f2eae2fa9f5f5ada87deba522a3bccd0016
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 728c8f94fdaeb28d6c915e1e6044df350c1ff53eb17c5b71b15747d1d154a536ce37c4b87758db07ddfa07368052752c60a1b468293e402ae11d19d5a995723b
|
7
|
+
data.tar.gz: 08f7deed942d838ab407aea293a7a5762f1fa40cbbf45680ca56d43dfb16bc5b8376402c8ec1b855d4f0de1a5afec89969d684b1fd624f8751e86a447a6aca37
|
@@ -0,0 +1,196 @@
|
|
1
|
+
class FormulaDifficultyCalculator
|
2
|
+
def initialize(formula)
|
3
|
+
@formula = formula
|
4
|
+
end
|
5
|
+
|
6
|
+
def difficulty
|
7
|
+
difficulty = 0
|
8
|
+
@formula.operations.each do |operation|
|
9
|
+
constant1, constant2 = get_constants(operation)
|
10
|
+
difficulty = difficulty +
|
11
|
+
build_operation_difficulty_calculator(operation.operator,
|
12
|
+
constant1,
|
13
|
+
constant2)
|
14
|
+
end
|
15
|
+
difficulty
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def build_operation_difficulty_calculator(operator, numbers1, numbers2)
|
21
|
+
klass = case operator
|
22
|
+
when :+ then AdditionOperationDifficultyCalculator
|
23
|
+
when :* then MultiplicationOperationDifficultyCalculator
|
24
|
+
when :/ then DivisionOperationDifficultyCalculator
|
25
|
+
when :- then SubtractionOperationDifficultyCalculator
|
26
|
+
end
|
27
|
+
klass.new(numbers1, numbers2).compute_difficulty
|
28
|
+
end
|
29
|
+
|
30
|
+
def get_constants(operation)
|
31
|
+
constants = []
|
32
|
+
constants << to_int_array(operation.constant1.value.to_i)
|
33
|
+
constants << to_int_array(operation.constant2.value.to_i)
|
34
|
+
end
|
35
|
+
|
36
|
+
def to_int_array(integer)
|
37
|
+
result_array = []
|
38
|
+
while integer > 0
|
39
|
+
result_array.unshift integer % 10
|
40
|
+
integer /= 10
|
41
|
+
end
|
42
|
+
result_array
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
class OperationDifficultyCalculator
|
47
|
+
attr_reader :difficulty
|
48
|
+
def initialize(numbers1, numbers2)
|
49
|
+
@numbers1 = numbers1
|
50
|
+
@numbers2 = numbers2
|
51
|
+
@difficulty = 0
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
class SubtractionOperationDifficultyCalculator <
|
56
|
+
OperationDifficultyCalculator
|
57
|
+
DIFFICULTIES = {
|
58
|
+
digit_zero: 1, # difference of zero and any digit
|
59
|
+
same_digits: 1, # difference of same values digits
|
60
|
+
even_even: 2, # difference of even digits
|
61
|
+
odd_odd: 2, # difference of odd digits
|
62
|
+
even_odd: 3, # difference of even and odd digits
|
63
|
+
borrow: 2, # doing a borrow
|
64
|
+
twodigit_digit: 4 # difference of two-digit number and digit
|
65
|
+
}
|
66
|
+
|
67
|
+
def compute_difficulty
|
68
|
+
if @numbers1.length < @numbers2.length
|
69
|
+
@difficulty += DIFFICULTIES[:borrow]
|
70
|
+
end
|
71
|
+
@numbers1.reverse.each_with_index do | a , index |
|
72
|
+
b = @numbers2.reverse[index]
|
73
|
+
case
|
74
|
+
when a == nil || b == nil
|
75
|
+
@difficulty += DIFFICULTIES[:digit_zero]
|
76
|
+
when a == 0 || b == 0
|
77
|
+
if a == 0
|
78
|
+
@difficulty += DIFFICULTIES[:borrow]
|
79
|
+
end
|
80
|
+
@difficulty += DIFFICULTIES[:digit_zero]
|
81
|
+
when a == b
|
82
|
+
@difficulty += DIFFICULTIES[:same_digits]
|
83
|
+
when a < b
|
84
|
+
@difficulty += DIFFICULTIES[:borrow]
|
85
|
+
else
|
86
|
+
@difficulty += DIFFICULTIES[:even_odd]
|
87
|
+
end
|
88
|
+
end
|
89
|
+
@difficulty
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
class DivisionOperationDifficultyCalculator <
|
94
|
+
OperationDifficultyCalculator
|
95
|
+
DIFFICULTIES = {
|
96
|
+
digit_zero: 0,
|
97
|
+
digit_one: 1,
|
98
|
+
digit_two: 2,
|
99
|
+
digit_other: 3
|
100
|
+
}
|
101
|
+
|
102
|
+
def compute_difficulty
|
103
|
+
@numbers1.reverse.each_with_index do | _ , index |
|
104
|
+
b = @numbers2.reverse[index]
|
105
|
+
case b
|
106
|
+
when 0
|
107
|
+
@difficulty += DIFFICULTIES[:digit_zero]
|
108
|
+
when 1
|
109
|
+
@difficulty += DIFFICULTIES[:digit_one]
|
110
|
+
when 2
|
111
|
+
@difficulty += DIFFICULTIES[:digit_two]
|
112
|
+
else
|
113
|
+
@difficulty += DIFFICULTIES[:digit_other]
|
114
|
+
end
|
115
|
+
end
|
116
|
+
@difficulty = @difficulty * @numbers2.length
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
class MultiplicationOperationDifficultyCalculator <
|
121
|
+
OperationDifficultyCalculator
|
122
|
+
DIFFICULTIES = {
|
123
|
+
digit_zero: 0,
|
124
|
+
digit_one: 1,
|
125
|
+
digit_two: 2,
|
126
|
+
digit_other: 3
|
127
|
+
}
|
128
|
+
|
129
|
+
def compute_difficulty
|
130
|
+
if @numbers1.length >= @numbers2.length
|
131
|
+
compare(@numbers1, @numbers2)
|
132
|
+
else
|
133
|
+
compare(@numbers2, @numbers1)
|
134
|
+
end
|
135
|
+
@difficulty
|
136
|
+
end
|
137
|
+
|
138
|
+
private
|
139
|
+
|
140
|
+
def compare(numbersa, numbersb)
|
141
|
+
numbersa.reverse.each_with_index do | a , index |
|
142
|
+
b = numbersb.reverse[index]
|
143
|
+
@difficulty = add_multiplication_difficulties(a)
|
144
|
+
@difficulty = add_multiplication_difficulties(b)
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
def add_multiplication_difficulties(number)
|
149
|
+
case number
|
150
|
+
when 0 || nil
|
151
|
+
@difficulty += DIFFICULTIES[:digit_zero]
|
152
|
+
when 1
|
153
|
+
@difficulty += DIFFICULTIES[:digit_one]
|
154
|
+
when 2
|
155
|
+
@difficulty += DIFFICULTIES[:digit_two]
|
156
|
+
else
|
157
|
+
@difficulty += DIFFICULTIES[:digit_other]
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
class AdditionOperationDifficultyCalculator < OperationDifficultyCalculator
|
163
|
+
DIFFICULTIES = {
|
164
|
+
digit_zero: 0, # any digit added to zero
|
165
|
+
under_ten: 3, # sum of even digits
|
166
|
+
ten: 2, # sum of odd digits # sum of even and odd digits
|
167
|
+
above_ten: 4 # difficulty of carry (for remembering and then adding)
|
168
|
+
}
|
169
|
+
|
170
|
+
def compute_difficulty
|
171
|
+
if @numbers1.length >= @numbers2.length
|
172
|
+
compare(@numbers1, @numbers2)
|
173
|
+
else
|
174
|
+
compare(@numbers2, @numbers1)
|
175
|
+
end
|
176
|
+
@difficulty
|
177
|
+
end
|
178
|
+
|
179
|
+
def compare(numbersa, numbersb)
|
180
|
+
numbersa.reverse.each_with_index do |a , index|
|
181
|
+
b = numbersb.reverse[index]
|
182
|
+
case
|
183
|
+
when a == nil || b == nil
|
184
|
+
@difficulty += DIFFICULTIES[:digit_zero]
|
185
|
+
when a == 0 || b == 0
|
186
|
+
@difficulty += DIFFICULTIES[:digit_zero]
|
187
|
+
when (a + b) == 10
|
188
|
+
@difficulty += DIFFICULTIES[:ten]
|
189
|
+
when (a + b) > 10
|
190
|
+
@difficulty += DIFFICULTIES[:above_ten]
|
191
|
+
else
|
192
|
+
@difficulty += DIFFICULTIES[:under_ten]
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end
|
196
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
class Formula
|
2
|
+
attr_reader :result, :operations
|
3
|
+
|
4
|
+
def initialize(operations)
|
5
|
+
@operations = operations
|
6
|
+
end
|
7
|
+
|
8
|
+
def text
|
9
|
+
@operations.map(&:text).join(" \n ")
|
10
|
+
end
|
11
|
+
|
12
|
+
def result
|
13
|
+
@operations.last.result
|
14
|
+
end
|
15
|
+
|
16
|
+
def difficulty
|
17
|
+
if @operations.length > 0
|
18
|
+
FormulaDifficultyCalculator.new(self).difficulty
|
19
|
+
else
|
20
|
+
nil
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class Constant
|
26
|
+
def initialize(value)
|
27
|
+
@value = value
|
28
|
+
end
|
29
|
+
|
30
|
+
def value
|
31
|
+
if @value.is_a?(Operation)
|
32
|
+
@value.result
|
33
|
+
else
|
34
|
+
@value
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
class Operation
|
40
|
+
attr_reader :constant1, :constant2, :operator
|
41
|
+
def initialize(operator, constant1, constant2)
|
42
|
+
@operator = operator
|
43
|
+
@constant1 = constant1
|
44
|
+
@constant2 = constant2
|
45
|
+
end
|
46
|
+
|
47
|
+
def text
|
48
|
+
"#{@constant1.value} #{@operator} #{@constant2.value}"
|
49
|
+
end
|
50
|
+
|
51
|
+
def result
|
52
|
+
@constant1.value.send(@operator, @constant2.value)
|
53
|
+
end
|
54
|
+
end
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hypatia
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Marthyn Olthof
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-05 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.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
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.2.1
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 10.2.1
|
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.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.0
|
55
|
+
description: A gem for determining the difficulty of a basic mathematical operation
|
56
|
+
email:
|
57
|
+
- Marthyn@live.nl
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- lib/hypatia/difficulty-calculator.rb
|
63
|
+
- lib/hypatia/formula.rb
|
64
|
+
homepage: http://github.com/marthyn/hypatia
|
65
|
+
licenses:
|
66
|
+
- MIT
|
67
|
+
metadata: {}
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options: []
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
requirements: []
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 2.4.5
|
85
|
+
signing_key:
|
86
|
+
specification_version: 4
|
87
|
+
summary: A gem for determining the difficulty of a basic mathematical operation
|
88
|
+
test_files: []
|
89
|
+
has_rdoc:
|