[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Fwd: Please Forward: Ruby Quiz Submission

James Gray

9/12/2007 12:42:00 PM

Begin forwarded message:

> From: Paolo Bonzini <bonzini@gnu.org>
> Date: September 12, 2007 7:31:31 AM CDT
> To: submission@rubyquiz.com
> Subject: Please Forward: Ruby Quiz Submission
>
> This is a solution for rubyquiz #122 (credit card).
>
> class String
> def luhn
> (split(//).inject([0,0]) {
> |(s1,s2), ch| c=ch.to_i; [s2 + c, s1 + (c*2%10) + (c*2/10)]
> }) [0] % 10 == 0
> end
> end
>
> mapn is probably more elegant, but this one does not use reverse,
> which is pretty cool IMO. :-)
>
> Paolo