Created
July 23, 2012 16:16
-
-
Save joshuacronemeyer/3164482 to your computer and use it in GitHub Desktop.
Here is my solution for the greed problem from the edgecase Koans https://github.com/edgecase/ruby_koans
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GreedScorer | |
def initialize(dice) | |
@dice = dice | |
end | |
def score | |
the_1000_point_score + other_triple_score + score_for_ones + score_for_fives | |
end | |
private | |
def the_1000_point_score | |
the_triple == 1 ? 1000 : 0 | |
end | |
def other_triple_score | |
the_triple > 1 ? the_triple * 100 : 0 | |
end | |
def score_for_ones | |
@dice.count(1)%3 * 100 | |
end | |
def score_for_fives | |
@dice.count(5)%3 * 50 | |
end | |
def the_triple | |
value, = @dice.group_by{|i| i}.find {|value, rolls| rolls.size >= 3} | |
value || 0 | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GreedScorerTest < Test::Unit::TestCase | |
def test_three_ones_1000_points | |
assert_equal 1000, GreedScorer.new([2,1,2,1,1]).score | |
end | |
def test_three_of_any_number_yields_100_times_that_number_points | |
assert_equal 200, GreedScorer.new([2,2,2,4,4]).score | |
assert_equal 300, GreedScorer.new([3,3,3,4,4]).score | |
assert_equal 400, GreedScorer.new([4,4,4,2,2]).score | |
assert_equal 500, GreedScorer.new([5,5,5,4,4]).score | |
assert_equal 600, GreedScorer.new([6,6,6,4,4]).score | |
end | |
def test_a_one_is_worth_one_hundred_points | |
assert_equal 100, GreedScorer.new([2,1,2,4,4]).score | |
assert_equal 200, GreedScorer.new([2,1,1,4,4]).score | |
assert_equal 1100, GreedScorer.new([2,1,1,1,1]).score | |
assert_equal 1200, GreedScorer.new([1,1,1,1,1]).score | |
end | |
def test_a_five_is_worth_fifty_points | |
assert_equal 50, GreedScorer.new([2,5,2,4,4]).score | |
assert_equal 100, GreedScorer.new([2,5,5,4,4]).score | |
assert_equal 550, GreedScorer.new([2,5,5,5,5]).score | |
assert_equal 600, GreedScorer.new([5,5,5,5,5]).score | |
end | |
def test_mix_of_scoring_outcomes | |
assert_equal 700, GreedScorer.new([1,1,5,5,5]).score | |
assert_equal 250, GreedScorer.new([5,1,6,6,1]).score | |
assert_equal 1150, GreedScorer.new([5,1,1,1,1]).score | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment