-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercise_generator.rb
More file actions
61 lines (56 loc) · 2.14 KB
/
exercise_generator.rb
File metadata and controls
61 lines (56 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
require 'exercise.rb'
module MyExerciseGenerator
OPERATION_TYPE ||= Hash["1" => "addition", "2" => "subtraction", "3" => "division", "4" => "multiplication"]
class ExerciseGenerator
def initialize(count)
@count = count
end
def get_random_operation
return OPERATION_TYPE[rand(1..4).to_s]
end
def first_num_greater
if rand(1) == 0
return true
else
return false
end
end
def generate
ex_array = Array.new()
for index in 0 .. @count-1
operation = get_random_operation
first = 0
second = 0
case operation
when "addition"
if first_num_greater
first = rand(2..99)
second = rand(1..100-first)
else
second = rand(2..99)
first = rand(1..100-second)
end
ex_array[index] = MyExercise::Exercise.new("%d + %d = ?" % [first, second], first + second, index+1)
when "subtraction"
first = rand(2..99)
second = rand(1..first-1)
ex_array[index] = MyExercise::Exercise.new("%d - %d = ?" % [first, second], first - second, index+1)
when "division"
second = rand(2..10)
first = second * rand(2..9)
ex_array[index] = MyExercise::Exercise.new("%d / %d = ?" % [first, second], first / second, index+1)
when "multiplication"
if first_num_greater
first = rand(2..9)
second = rand(1..first-1)
else
second = rand(2..9)
first = rand(1..second-1)
end
ex_array[index] = MyExercise::Exercise.new("%d * %d = ?" % [first, second], first * second, index+1)
end
end
return ex_array
end
end
end