Begin forwarded message: > From: "Harry Kakueki" <list.push / gmail.com> > Date: May 2, 2007 10:46:47 PM CDT > To: "James Edward Gray II" <james / grayproductions.net> > Subject: Re: Ruby Quiz solution > > On 5/3/07, James Edward Gray II <james / grayproductions.net> wrote: >> On May 2, 2007, at 8:27 PM, Harry Kakueki wrote: >> >> > I submitted a solution to Ruby Quiz a few minutes after the end >> of the >> > No-Spoiler period but my name is not listed under Quiz Solutions. >> > Does that mean there is something wrong with my solution? >> >> I'm sorry Harry. I don't see the solution on Ruby Talk or in my >> personal mail. If you resend I'll sure watch for it. Sorry I missed >> it. >> >> James Edward Gray II >> > > No problem. > I just wanted to understand what happened. > Thanks. > > Here is my solution. > > ###### > # Check Starting numbers and length > def card_ok(str,info) > check = "UNKNOWN" > info.each do |t| > pref = t[0] > leng = t[1] > name = t[2] > pref.each do |x| > if x == str.slice(0...x.length) > leng.each do |y| > if y.to_i == str.length > check = name.dup > end > end > end > end > end > return check > end > > # Check Luhn algorithm > def luhn(str) > luhn_hash = Hash.new("INVALID") > if str.length != 0 > luhn_hash[0] = "VALID" > arr = str.split(//).reverse > arr2 = [] > arr3 = [] > (0...arr.length).each do |u| > arr2 << arr[u] if u %2 == 0 > arr2 << (arr[u].to_i * 2).to_s if u %2 != 0 > end > > arr2.each do |r| > arr3 << r.split(//).inject(0) {|sum,i| sum + i.to_i} > end > > val = arr3.inject(0) {|sum,i| sum + i} % 10 > end > return luhn_hash[val] > end > > # Card information > test = [] > test << [["31","34"],["15"],"AMEX"] > test << [["6011"],["16"],"DISCOVER"] > test << [("51".."55").to_a,["16"],"MASTERCARD"] > test << [["4"],["13","16"],"VISA"] > #test << [("3528".."3589").to_a,["16"],"JCB"] > #test << [[("3000".."3029").to_a + ("3040".."3059").to_a + > #("3815".."3889").to_a + ["36","389"]].flatten!,["14"],"DINERS"] > > # Main > str = ARGV.join > arr = [] > arr << card_ok(str,test) > arr << luhn(str) > puts arr > > Harry > > -- > http://www.kakueki.com/ruby/list.html > A Look into Japanese Ruby List in English