Sunday, August 7, 2011

Ruby Koan for Dice Scoring (Greed) - Solution #2

It's interesting what some sleep and exercise can do.  The next day while approaching the "big hill" part of my run I came up with a new solution to the Greed Ruby Koan.  This solution is shorter and is more Rubyesque (I think). It also includes a reasonable amount of error checking (nil parameter and valid dice values).  Again, I'd love to hear from Ruby enthusiasts to comment on style and other approaches.

def score(dice)

  return 0 if dice == nil

  score  = 0
  counts = [0, 0, 0, 0, 0, 0]
  dice.each { |roll| counts[roll-1] += 1 if (1..6) === roll } # Count valid rolls

  (1..6).each { |i| score += (i == 1) ? 1000 : i * 100 if counts[i-1] >= 3 } # Score triples

  # Score the rest
  score += counts[0] % 3 * 100
  score += counts[4] % 3 * 50

  return score
end

39 comments:

./scc said...

Not bad Peter - second solution definitely more block/closure oriented. Looking forward to more Ruby critiques ;)

Mike Ewing said...

The use of % is a very nice touch I had not thought of. I've included it in my solution. You can see that I'm using the built-in count method that is available on arrays. You don't need to do the counting yourself. I've also omitted error checking (TDD says let the tests guide the code) and the return statement, which is unnecessary.

def score(dice)

score = 0
score += 1000 if dice.count(1) >= 3
(2..6).each { |pips| score += pips * 100 if dice.count(pips) >= 3 }
score += 100 * (dice.count(1) % 3)
score += 50 * (dice.count(5) % 3)

end

Ashley Cruz said...

Then gold was found at neighboring Nundle and Tamworth began increase by leaps and bounds. The secular labels, with more mainstream artists, theyll figure out how to make money and then the Christian labels will follow suit. Resort Records is a company that has been in business for over 15 years. Country music groups Lady Antebellum

Anonymous said...

First of all I would like to say terrific blog! I had a quick question in which I'd like to ask if you do not mind. I was interested to find out how you center yourself and clear your mind before writing. I've
had a tough time clearing my thoughts in getting my thoughts out
there. I do enjoy writing however it just seems like the first 10 to 15 minutes are generally lost simply just trying to figure out how to begin.
Any suggestions or tips? Kudos!
Feel free to visit my site ... partygalleries.com

Anonymous said...

It's going to be end of mine day, however before finish I am reading this wonderful paragraph to increase my knowledge.
Here is my site - nude teens

Anonymous said...

Thanks for the marvelous posting! I seriously enjoyed reading it, you can be a great author.
I will make certain to bookmark your blog and will eventually
come back sometime soon. I want to encourage yourself to continue
your great job, have a nice evening!
My website :: http://www.daily-wet-tshirt.com/

Anonymous said...

Whats up this is somewhat of off topic but I was wanting to know if blogs use WYSIWYG editors or if you have
to manually code with HTML. I'm starting a blog soon but have no coding expertise so I wanted to get guidance from someone with experience. Any help would be enormously appreciated!
Also see my web site > Naked Girls

Anonymous said...

Hello there! I simply wish to offer you a big thumbs up for the great info you have got
right here on this post. I'll be coming back to your website for more soon.
Also see my site: www.crazyteenpics.com

Anonymous said...

Hey! Would you mind if I share your blog with my twitter group?
There's a lot of folks that I think would really enjoy your content. Please let me know. Cheers
My web page :: Stunning young brunette Janessa Price rides big-dick to orgasm

Anonymous said...

I like the valuable info you provide in your articles.
I'll bookmark your blog and check again here regularly. I am quite certain I'll learn plenty of new stuff
right here! Best of luck for the next!
My webpage - wendy's shifting castle walkthrough

Anonymous said...

Malaysia & Singapore & brunei best online blogshop for wholesale & quantity korean
add-ons, earrings, earstuds, choker, rings, hair, trinket & bangle add-ons.

Deal 35 % wholesale markdown. Ship Worldwide
Feel free to surf my web page : locksmith slc

Anonymous said...

Malaysia & Singapore & brunei finest on the internet blogshop for wholesale & quantity korean add-ons, accessories, earstuds, necklace, rings,
bracelet, bangle & hair accessories. Promotion 35 % wholesale markdown. Ship Worldwide
Here is my blog post - locksmith slc

Anonymous said...

Malaysia & Singapore & brunei finest internet blogshop for wholesale & quantity korean add-ons, accessories, earstuds, pendant, rings, bracelet, hair & bracelet add-ons.
Offer 35 % wholesale markdown. Ship Worldwide
Feel free to surf my web page :: online music promotion

Anonymous said...

Woah! I'm really enjoying the template/theme of this blog. It's simple, yet effective.
A lot of times it's difficult to get that "perfect balance" between user friendliness and visual appearance. I must say you have done a great job with this. In addition, the blog loads extremely quick for me on Firefox. Excellent Blog!
Also visit my webpage : property management company

Anonymous said...

It's a shame you don't have a donate button! I'd most certainly donate to this excellent blog! I suppose for now i'll settle for
book-marking and adding your RSS feed to my Google
account. I look forward to fresh updates and will talk about this site with my Facebook group.
Chat soon!
my website > Trick Photography

Anonymous said...

If you would like to grow your familiarity only keep visiting this web site and be updated
with the newest news posted here.
Review my blog post ... home renovation near orlando florida

Anonymous said...

Hola! I've been following your blog for a while now and finally got the bravery to go ahead and give you a shout out from Porter Tx! Just wanted to tell you keep up the excellent job!
My web page :: click through the up coming document

Anonymous said...

What's up i am kavin, its my first occasion to commenting anywhere, when i read this article i thought i could also make comment due to this good article.
My homepage - Excellent Benefits

Anonymous said...

Hello, every time i used to check webpage posts here early in the daylight, because i enjoy to
find out more and more.

Also visit my web blog :: BMW Z4 Windscreen
My web page :: BMW Z4 Windscreen

Anonymous said...

Good web site you have got here.. It's hard to find excellent writing like yours these days. I really appreciate individuals like you! Take care!!

Feel free to visit my web page gauba
my webpage > nikon digital camera

Anonymous said...

Hi, after reading this awesome paragraph i am too
delighted to share my know-how here with friends.

Visit my web page peoples quotes

Anonymous said...

I know this site provides quality dependent posts and other
stuff, is there any other web site which provides these kinds of data in quality?


my web site d'nest Condo
My web site :: d'nest Condo

Anonymous said...

When someone writes an article he/she maintains the plan of a user in his/her mind that how a user can understand it.
Therefore that's why this paragraph is perfect. Thanks!

My blog HCG Diet

Anonymous said...

I was able to find good information from your blog articles.


My web site ... HCG Diet

Anonymous said...

Hey there! I've been following your web site for a long time now and finally got the bravery to go ahead and give you a shout out from Humble Texas! Just wanted to say keep up the good job!

Here is my blog post :: kitchen cabinet

Anonymous said...

Hmm is anyone else having problems with the pictures on this blog loading?
I'm trying to figure out if its a problem on my end or if it's the blog.
Any responses would be greatly appreciated.

Here is my site ... Http://Www.Onhiddencam.Info

Anonymous said...

Great website you have here but I was wondering if you knew
of any user discussion forums that cover the same topics talked about here?
I'd really love to be a part of group where I can get feedback from other knowledgeable people that share the same interest. If you have any recommendations, please let me know. Thank you!

my blog post :: sexygirlchat.net

Anonymous said...

Having read this I believed it was extremely informative.
I appreciate you finding the time and effort to
put this article together. I once again find myself
spending a lot of time both reading and posting comments.
But so what, it was still worthwhile!

Also visit my web-site - http://www.teenpornpost.com

chen jing said...

When some one searches for his essential thing, so he/she desires to be available that in detail, thus that thing is maintained over
here.

Here is my weblog - taobao singapore

chen jing said...

When some one searches for his essential thing, so he/she desires to be available that in detail, thus that thing is maintained over
here.

Here is my weblog - taobao

chen jing said...

When some one searches for his essential thing, so he/she desires to be available that in detail, thus that thing is maintained over
here.

Here is my weblog - taobao agent

Kirsten Jones said...

Vous voulez être bénis d'apercevoir robinetterie-baignoire que vous pouvez accepter une ablution orné dans votre maison à un prix abordable. Par les dépenses douche un peu ajouté de l'argent, vous pouvez tranquillement obtenir une ablution de vos rêves après avoir douche-robinetterie-douche accepté extravagant.

Anonymous said...

Myself, I'll stick with rimless eyeglasses nerd chic looking, but I adore that FinestGlasses.COM bring up rimless glasses rimless eyeglasses in differ fashion design. More attractive with sexy feeling!!!

Anonymous said...

For instance, inflammations of the bladder are known to trigger cats to spray. cat eye glasses we can see the elegance of this globe and wholesome eyes also make the encounter beautiful in a human body.

Anonymous said...

Taps UK- Top quality, Great value bathroom and kitchen sinks and taps from our online shop. Lowest Prices On Bathroom Suites,Baths,Taps,Showers,Furniture. Price Guarantee - Next Day Delivery Available -www.uktaps.co.uk FREE Delivery over £199.

sherry jiang said...

Waterfall taps are a stylish way to make a statement in your bathroom, and are becoming increasingly popular and very affordable. Water cascades from an open spout creating a stunning waterfall effect that makes washing, shaving and cleaning your teeth something to look forward to. Plumpinguk.co.uk offers a wide range of waterfall basin mixers all at discounted prices, so adding that wow-factor has never been easier.http://www.plumpinguk.co.uk/bathroom-sink-taps/waterfall-taps.html

sherry jiang said...

Waterfall taps are a stylish way to make a statement in your bathroom, and are becoming increasingly popular and very affordable. Water cascades from an open spout creating a stunning waterfall effect that makes washing, shaving and cleaning your teeth something to look forward to. Plumpinguk.co.uk offers a wide range of waterfall basin mixers all at discounted prices, so adding that wow-factor has never been easier.http://www.plumpinguk.co.uk/bathroom-sink-taps/waterfall-taps.html

sherry jiang said...

Waterfall taps are a stylish way to make a statement in your bathroom, and are becoming increasingly popular and very affordable. Water cascades from an open spout creating a stunning waterfall effect that makes washing, shaving and cleaning your teeth something to look forward to. Plumpinguk.co.uk offers a wide range of waterfall basin mixers all at discounted prices, so adding that wow-factor has never been easier.http://www.plumpinguk.co.uk/bathroom-sink-taps/waterfall-taps.html

Cheap Tap said...



These days, homemakers want to add in more type, flair and superiority into their kitchens. As such, even the taps are chosen with more care and consideration. With this change in choice, you see the appearance of designer http://www.cheaptap.co.uk/waterfall-taps-c-36.html .