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

33 comments:

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

    ReplyDelete
  2. 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

    ReplyDelete
  3. 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

    ReplyDelete
  4. 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

    ReplyDelete
  5. 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

    ReplyDelete
  6. 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/

    ReplyDelete
  7. 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

    ReplyDelete
  8. 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

    ReplyDelete
  9. 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

    ReplyDelete
  10. 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

    ReplyDelete
  11. 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

    ReplyDelete
  12. 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

    ReplyDelete
  13. 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

    ReplyDelete
  14. 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

    ReplyDelete
  15. 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

    ReplyDelete
  16. 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

    ReplyDelete
  17. 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

    ReplyDelete
  18. 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

    ReplyDelete
  19. 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

    ReplyDelete
  20. 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

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

    Visit my web page peoples quotes

    ReplyDelete
  22. 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

    ReplyDelete
  23. 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

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


    My web site ... HCG Diet

    ReplyDelete
  25. 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

    ReplyDelete
  26. 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

    ReplyDelete
  27. 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

    ReplyDelete
  28. 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

    ReplyDelete
  29. 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

    ReplyDelete
  30. 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.

    ReplyDelete
  31. 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!!!

    ReplyDelete


  32. 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 .

    ReplyDelete
  33. Pretty good post. I have just stumbled upon your blog and enjoyed reading your blog posts very much. I am looking for new posts to get more precious info. Big thanks for the useful info. Malachite Dice

    ReplyDelete