Skip to content

Instantly share code, notes, and snippets.

@hakobe
Created July 26, 2008 07:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hakobe/2619 to your computer and use it in GitHub Desktop.
Save hakobe/2619 to your computer and use it in GitHub Desktop.
class Problem
attr_reader :x, :y, :op
def initialize(op, x, y)
@op = op
@x = x
@y = y
end
def to_s
"#{x} #{op} #{y}"
end
def answer
x.send(op, y)
end
end
if RUBY_VERSION < 1.8.7
class Array
def choice
self[ rand(self.size) ]
end
end
end
def create_easy_problem
op = [:+, :-, :*].choice
x = (0...10).to_a.choice
y = (0...10).to_a.choice
Problem.new(op, x, y)
end
def difficult_easy_problem
end
result = {
:good => 0,
:bad => 0,
}
100.times do |n|
puts "!! #{n} !!" if n % 10 == 0
problem = create_easy_problem
print problem, " = "
answer = gets.to_i
break unless answer
answer = answer.to_i
if answer == problem.answer
puts "good"
result[:good] += 1
else
puts "bad"
result[:bad] += 1
end
end
pp result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment