def score(dice) score = 0 points = Hash.new(0) points[1] = 100 points[5] = 50 counts = Hash.new(0) dice.each { |number| counts[number] += 1 } counts.each do |number, count| if count >= 3 if number == 1 score += 1000 else score += (number * 100) end count = count - 3 end score += points[number] * count end score end
If you're not familiar with the ruby koans, here's the link to the path of enlightenment: Ruby koans
You can use `inject` to load the hash and a case statement to iterate through the possible die sides:
ReplyDeletedef score(dice)
tally = 0
return tally if dice.length == 0
hash = dice.inject(Hash.new(0)) { |h,v| h[v] += 1; h }
(1..6).collect do |roll|
case roll
when 5
tally += (hash[roll] / 3) * 500 + (hash[roll] % 3) * 50
when 1
tally += (hash[roll] / 3) * 1000 + (hash[roll] % 3) * 100
else
tally += (hash[roll] / 3) * roll * 100
end
end
ap "dice = #{dice}, " + "hash = #{hash}, " + "tally = #{tally}"
tally
end
Great Article Artificial Intelligence Projects
DeleteProject Center in Chennai
JavaScript Training in Chennai
JavaScript Training in Chennai
Nice stuff thanks for sharing
ReplyDeletelinux classes in chennai | rhce classes in chennai | red hat classes in chennai red hat linux training in chennai
Hi dear, I like your post so much, and it is very good. Hope that we can communicate with each other. I want to introduce an aso services, I try it and I feel it is so good to us beginners.
ReplyDeleteThis was really interesting info in this blog that to very happy for the nice technology in this blog. best live gambling
ReplyDeletethanks for sharing,im really interested with this site :
ReplyDeleteagen ion casino online
http://iya-poker.site/
agen qqpoker online resmi
iyapoker
bandarasia
whatsapp group links
ReplyDeletesolarmovie
ReplyDelete1movies
ReplyDeletereal estate whatsapp groups
ReplyDeleteThanks for sharing your innovative ideas to our vision. I have read your blog and I gathered some new information through your blog. Your blog is really very informative and unique. Keep posting like this. Awaiting for your further update.If you are looking for any Data science related information, please visit our website Data science training institute in bangalore
ReplyDeleteMust needed article very helpful..Keep posting many more.
ReplyDeleteprofessional web design services
Very useful blog. In this blog, I got so much useful information.
ReplyDeletearchitecture of selenium
angularjs future
aws certification validity
android 8.0 oreo
aws interview questions and answers