forked from gfmurphy/luhnybin
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
first attempt at recursive solution
- Loading branch information
Showing
2 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#!/bin/sh | ||
|
||
# Call your program here instead of cat. | ||
cat | ||
./luhnybin.rb |