Created
January 15, 2014 00:18
-
-
Save RyanHedges/8428559 to your computer and use it in GitHub Desktop.
greed is good
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
# My solution - | |
# http://www.codewars.com/dojo/katas/5270d0d18625160ada0000e4/play/ruby | |
def score( dice ) | |
points = 0 | |
triplet = {1=>1000,2=>200,3=>300,4=>400,5=>500,6=>600} | |
(1..6).each do |i| | |
total = dice.count(i) | |
if total >= 3 | |
points += triplet[i] | |
dice.delete(i) | |
#had to write this to address edge case. | |
dice << i if dice.length == 0 | |
dice << i if dice.length == 1 | |
end | |
end | |
dice.each do |num| | |
points += 100 if num == 1 | |
points += 50 if num == 5 | |
end | |
points | |
end | |
# This is the code that I saw in the solutions that seemed the most reasonable. | |
# It adjusts the scores in the hash to acount for 1 and 5 being triplets. | |
def score( dice ) | |
total = 0 | |
threes = { 1 => 700, 2 => 200, 3 => 300, 4 => 400, 5 => 350, 6 => 600 } | |
threes.each do |num, points| | |
total += points if dice.count(num) >= 3 | |
end | |
total += dice.count(1) * 100 | |
total += dice.count(5) * 50 | |
total | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Lines 13 and 14 can be written as
dice.length == 0 || dice.length == 1
I don't like that you modify the input array your method should be called score!.
Does your code work? I don't see how it would work since it doesn't delete all 3 numbers when a triplet is found. Unless I'm missing something.
Using count kinda sucks to me because it makes the complexity 6N when it should only really be N + 6