Skip to content
Snippets Groups Projects
Commit f77ef362 authored by François Sarradin's avatar François Sarradin
Browse files

add new question for second phase

parent d60b8c0d
No related branches found
No related tags found
No related merge requests found
......@@ -154,6 +154,119 @@ module ExtremeStartup
end
end
class ListQuestion < GetQuestion
def random_numbers
randoms = Set.new
loop do
randoms << rand(1000)
return randoms.to_a if randoms.size >= 5
end
end
def candidate_numbers
(1..100).to_a
end
def generate_numbers(size)
random_numbers[0..size].concat(candidate_numbers.shuffle[0..size]).shuffle
end
def answer
@answer && @answer.downcase.strip.gsub(/[ \\t\\r\\n]*,[ \\t\\r\\n]*/, ',')
end
def answered_correctly?(answer)
correct_answer.to_s.downcase.gsub(/[ \\t\\r\\n]*,[ \\t\\r\\n]*/, ',') == answer
end
end
class ReverseListQuestion < ListQuestion
def initialize(player, *numbers)
if numbers.any?
@numbers = numbers
else
size = rand(5)
@numbers = generate_numbers(size)
end
end
def as_text
"reverse this list: " + @numbers.join(', ')
end
private
def correct_answer
@numbers.reverse.join(',')
end
end
class SumListQuestion < ListQuestion
def initialize(player, *numbers)
if numbers.any?
@numbers = numbers
else
size = rand(4) + 1
@numbers = generate_numbers(size)
end
end
def as_text
"give the sum of elements in: " + @numbers.join(', ')
end
private
def correct_answer
@numbers.reduce(:+)
end
end
class MultiplyElementsInListQuestion < ListQuestion
def initialize(player, *numbers)
if numbers.any?
@n = numbers[0]
@numbers = *numbers[1..-1]
else
@n = rand(1000)
size = rand(5)
@numbers = generate_numbers[0..size].concat(candidate_numbers.shuffle[0..size]).shuffle
end
end
def as_text
"multiply by #{@n} those elements: " + @numbers.join(', ')
end
private
def correct_answer
@numbers.map { |e| e * @n }.join(',')
end
end
class FibonacciListQuestion < ListQuestion
def initialize(player, *numbers)
if numbers.any?
@n = numbers[0] + 1
else
@n = rand(19) + 1
end
end
def as_text
count = "are the #{@n} first numbers"
if (@n == 1)
count = "is the first number"
end
"what #{count} of the Fibonacci sequence"
end
private
def fibonacci(n)
a, b = 0, 1
n.times { a, b = b, a + b }
a
end
def correct_answer
(1..@n).map { |e| fibonacci(e) }.join(',')
end
end
class MaximumQuestion < SelectFromListOfNumbersQuestion
def as_text
"which of the following numbers is the largest: " + @numbers.join(', ')
......@@ -459,6 +572,34 @@ module ExtremeStartup
end
class SecondPhaseQuestionFactory < GetQuestion
attr_reader :round
def initialize
@round = 1
@question_types = [
ReverseListQuestion,
SumListQuestion,
MultiplyElementsInListQuestion,
FibonacciListQuestion
]
end
def available_question_types
window_end = (@round * 2 - 1)
window_start = [0, window_end - 4].max
@question_types[window_start..window_end]
end
def next_question(player)
available_question_types.sample.new(player)
end
def advance_round
@round += 1
end
end
class WarmupQuestion < GetQuestion
def initialize(player)
@player = player
......
require 'spec_helper'
require 'extreme_startup/question_factory'
require 'extreme_startup/player'
module ExtremeStartup
describe FibonacciListQuestion do
let(:question) { FibonacciListQuestion.new(Player.new, 17) }
it "converts to a string" do
question.as_text.should =~ /what are the 18 first numbers of the Fibonacci sequence/i
end
context "when the numbers are known" do
let(:question) { FibonacciListQuestion.new(Player.new, 5) }
it "converts to the right string" do
question.as_text.should =~ /what are the 6 first numbers of the Fibonacci sequence/i
end
it "identifies a correct answer" do
question.answered_correctly?("1,1,2,3,5,8").should be_true
end
it "identifies an incorrect answer" do
question.answered_correctly?("1,1,2,3,5").should be_false
end
end
end
end
require 'spec_helper'
require 'extreme_startup/question_factory'
require 'extreme_startup/player'
module ExtremeStartup
describe MultiplyElementsInListQuestion do
let(:question) { MultiplyElementsInListQuestion.new(Player.new) }
it "converts to a string" do
question.as_text.should =~ /multiply by \d+ those elements: \(\d+(, \d+)*\)/i
end
context "when the numbers are known" do
let(:question) { MultiplyElementsInListQuestion.new(Player.new, 2, 1, 2, 3) }
it "converts to the right string" do
question.as_text.should =~ /multiply by 2 those elements: \(1, 2, 3\)/i
end
it "identifies a correct answer" do
question.answered_correctly?("2, 4, 6").should be_true
end
it "identifies an incorrect answer" do
question.answered_correctly?("1, 2, 3").should be_false
end
end
end
end
require 'spec_helper'
require 'extreme_startup/question_factory'
require 'extreme_startup/player'
module ExtremeStartup
describe SumListQuestion do
let(:question) { SumListQuestion.new(Player.new) }
it "converts to a string" do
question.as_text.should =~ /give the sum of elements in: \(\d+(, \d+)*\)/i
end
context "when the numbers are known" do
let(:question) { SumListQuestion.new(Player.new, 1, 2, 3) }
it "converts to the right string" do
question.as_text.should =~ /give the sum of elements in: \(1, 2, 3\)/i
end
it "identifies a correct answer" do
question.answered_correctly?("6").should be_true
end
it "identifies an incorrect answer" do
question.answered_correctly?("42").should be_false
end
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment