Skip to content

Instantly share code, notes, and snippets.

@burke
Created July 25, 2009 05:42
Show Gist options
  • Save burke/154711 to your computer and use it in GitHub Desktop.
Save burke/154711 to your computer and use it in GitHub Desktop.
class Question
attr_accessor :question, :answer, :weight
def initialize(question, answer)
@question = question
@answer = answer
@weight = 5
end
def update_time(time)
@@average_time ||= time
if time > 5*@@average_time
time = 5*@@average_time
end
@@average_time = (0.8 * @@average_time) + (0.2 * time)
end
def fail(time)
update_time(time)
@weight += 10
end
def win(time)
update_time(time)
@weight += (time - @@average_time)
@weight -= 3 # for winning.
# just so we don't NEVER ask this question again...
@weight = 1 if @weight < 1
end
end
class QuestionList
def initialize
@list = []
end
def <<(o)
@list << o
end
# REALLY INEFFICIENT.
def size
@list.map(&:weight).inject(&:+)
end
# REALLY INEFFICIENT.
def get_weighted_random
rnd = rand(size)
@list.each do |el|
rnd -= el.weight
return el if rnd <= 0
end
end
end
class Quiz
def initialize(hash)
@ql = QuestionList.new
hash.each do |key, value|
@ql << Question.new(key, value)
end
end
def quiz(n=nil, reverse=false)
args = reverse ? [:answer, :question] : [:question, :answer]
if n
n.times { question(args) }
else
loop { question(args) }
end
end
def question(args=[:question,:answer])
q = @ql.get_weighted_random
puts q.send(args[0])
time_start = Time.now
correct = gets.chomp.downcase == q.send(args[1]).downcase
time = Time.now - time_start
if correct
puts "-------------"
q.win(time)
else
puts "---: NO! :--- #{q.send(args[1])}"
q.fail(time)
end
end
end
if __FILE__ == $0
amino = {
"Alanine" => "A",
"Valine" => "V",
"Leucine" => "L",
"Isoleucine" => "I",
"Proline" => "P",
"Methionine" => "M",
"Phenylalanine" => "F",
"Tryptophan" => "W",
"Glycine" => "G",
"Serine" => "S",
"Cysteine" => "C",
"Threonine" => "T",
"Asparagine" => "N",
"Glutamine" => "Q",
"Tyrosine" => "Y",
"Aspartic Acid" => "D",
"Glutamic Acid" => "E",
"Lysine" => "K",
"Arginine" => "R",
"Histidine" => "H"
}
Quiz.new(amino).quiz(nil,true)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment