Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
first attempt at recursive solution
  • Loading branch information
gfmurphy authored and George F Murphy committed Nov 18, 2011
1 parent da8ea44 commit 0c16903
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
76 changes: 76 additions & 0 deletions luhnybin.rb
@@ -0,0 +1,76 @@
#!/usr/bin/env ruby
class Luhnybin
MINIMUM = 14
SUMMED_DOUBLES = [0, 2, 4, 6, 8, 1, 3, 5, 7, 9].freeze

def initialize(text)
@text = text.unpack('c*')
filter
end

def text
@text.pack('c*')
end

private
def filter(start=0, index=0, digits=[])
return 0 if index == @text.length || digits.length > 16

count = luhn(digits)
return count if count > 0

char = @text[index]
digit = digit?(char)

if digit
value = char - ?0
digits.unshift(value)
elsif !separator?(char)
start = index
digits.clear
end

lc = filter(start, index + 1, digits)
if lc > 0 && digit
@text[index] = ?X
lc -= 1
end
return lc
end

def luhn(digits)
length = digits.length
return 0 if length < MINIMUM

length.downto(MINIMUM) do |n|
i = 0
sum = digits[0,n].reduce(0) do |tot, d|
tot += i.even? ? d : SUMMED_DOUBLES[d]
i += 1
tot
end
return n if sum % 10 == 0
end
return 0
end

def digit?(char)
char.between?(?0, ?9)
end

def separator?(char)
char == ?- || char == 32
end
end

class String
def mask_cc_number
Luhnybin.new(self).text
end
end

if __FILE__ == $0
STDIN.each do |line|
STDOUT << line.mask_cc_number
end
end
2 changes: 1 addition & 1 deletion mask.sh
@@ -1,4 +1,4 @@
#!/bin/sh

# Call your program here instead of cat.
cat
./luhnybin.rb

0 comments on commit 0c16903

Please sign in to comment.