Skip to content

Instantly share code, notes, and snippets.

@framallo
Created June 6, 2014 19:19
Show Gist options
  • Select an option

  • Save framallo/de66a9a23b5f4ff1f01f to your computer and use it in GitHub Desktop.

Select an option

Save framallo/de66a9a23b5f4ff1f01f to your computer and use it in GitHub Desktop.
scoring_peredictions
Write a method that accepts two arguments: an Array of five guesses for
finalists in a race and an Array of the five actual finalists. Each
position in the lists matches a finishing position in the race, so first
place corresponds to index 0. Return an Integer score of the predictions:
0 or more points. Correctly guessing first place is worth 15 points,
second is worth 10, and so on down with 5, 3, and 1 point for fifth
place. It's also worth 1 point to correctly guess a racer that finishes
in the top five but to have that racer in the wrong position.
require "race"
describe "Race::score" do
let(:winners) { %w[First Second Third Fourth Fifth] }
def correct_guesses(*indexes)
winners.map.with_index { |w, i| indexes.include?(i) ? w : "Wrong" }
end
it "add points for each position" do
Race.score(winners, winners).should eq(15 + 10 + 5 + 3 + 1)
end
it "gives 0 points for no correct guesses" do
all_wrong = correct_guesses # none correct
Race.score(all_wrong, winners).should eq(0)
end
it "gives 15 points for first place" do
Race.score(correct_guesses(0), winners).should eq(15)
end
it "gives 10 points for second place" do
Race.score(correct_guesses(1), winners).should eq(10)
end
it "gives 5 points for third place" do
Race.score(correct_guesses(2), winners).should eq(5)
end
it "gives 3 points for fourth place" do
Race.score(correct_guesses(3), winners).should eq(3)
end
it "gives 1 point for fifth place" do
Race.score(correct_guesses(4), winners).should eq(1)
end
it "gives one point for a correct guess in the wrong place" do
guesses = correct_guesses(0)
guesses.unshift(guesses.pop) # shift positions by one
Race.score(guesses, winners).should eq(1)
end
it "score positional and misplaced guesses at the same time" do
guesses = correct_guesses(0, 3)
guesses[3], guesses[4] = guesses[4], guesses[3]
Race.score(guesses, winners).should eq(15 + 1)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment