"To err is human; to forgive, divine." -Alexander Pope
Mistakes happen. At Square, we accept that human error is inevitable. We anticipate potential slip-ups and implement safety measures to mitigate—and oftentimes completely eliminate—any repercussions.
For example, Square's Luhn filter monitors logs and masks anything that looks like a credit card number. If a number like "4111 1111 1111 1111" were accidentally logged as part of an error message, our filter would replace it with "XXXX XXXX XXXX XXXX" and page an on call engineer.
The Luhn filter looks for sequences of digits that pass the Luhn check, a simple checksum algorithm invented by Hans Peter Luhn in 1954. All valid credit card numbers pass the Luhn check, thereby enabling computer programs, like our log filter, to distinguish credit card numbers from random digit sequences.
The Luhn check works like this:
For example, "5678" passes the Luhn check:
"6789" does not:
Write a command line program that reads ASCII text from standard input, masks sequences of digits that look like credit card numbers, and writes the filtered text to standard output. For the purposes of this challenge, a credit card number:
' ') and hyphens ('-').If a sequence of digits looks like a credit card number, replace each digit with an 'X'. Any characters, including digits, may flank a credit card number. Beware. Potential credit card numbers can overlap. A valid 16-digit number can even contain a valid 14 or 15-digit number. Your program must mask every digit.
I already wrote a test suite, so you can jump straight to the fun part: writing the algorithm. To participate:
mask.sh to call your program.run.sh.run.sh passes, post a link to your solution in the comments on our blog.Windows users should use Cygwin to run the tests. Please make it easy for others to check out and run your solution.
The first time you execute run.sh, you'll see a test failure:
$ ./run.sh
Running tests against mask.sh...
.X
Test #2 of 20 failed:
Description: valid 14-digit #
Input: 56613959932537\n
Expected result: XXXXXXXXXXXXXX\n
Actual result: 56613959932537\n
Modify mask.sh and make the tests pass. Line feeds delineate the test cases. If you pass a number on the command line, run.sh will repeat the test suite the specified number of times; this is useful for performance comparisons. The tests aren't set in stone—if you have an idea for improving the test suite, please submit a pull request.
This isn't a contest, but an innovative solution could score you interviews at Square. I'm primarily interested to see how different programming languages stack up with regard to readability and performance.
Once we have enough interesting submissions, I'll summarize the results in a followup blog post and open source our own Java-based implementation. In the mean time, if you enjoy working with talented people on challenging problems like this, email your résumé to luhnybin@squareup.com.
Good luck!
43 commits
Shell
100.0%
"To err is human; to forgive, divine." -Alexander Pope
Mistakes happen. At Square, we accept that human error is inevitable. We anticipate potential slip-ups and implement safety measures to mitigate—and oftentimes completely eliminate—any repercussions.
For example, Square's Luhn filter monitors logs and masks anything that looks like a credit card number. If a number like "4111 1111 1111 1111" were accidentally logged as part of an error message, our filter would replace it with "XXXX XXXX XXXX XXXX" and page an on call engineer.
The Luhn filter looks for sequences of digits that pass the Luhn check, a simple checksum algorithm invented by Hans Peter Luhn in 1954. All valid credit card numbers pass the Luhn check, thereby enabling computer programs, like our log filter, to distinguish credit card numbers from random digit sequences.
The Luhn check works like this:
For example, "5678" passes the Luhn check:
"6789" does not:
Write a command line program that reads ASCII text from standard input, masks sequences of digits that look like credit card numbers, and writes the filtered text to standard output. For the purposes of this challenge, a credit card number:
' ') and hyphens ('-').If a sequence of digits looks like a credit card number, replace each digit with an 'X'. Any characters, including digits, may flank a credit card number. Beware. Potential credit card numbers can overlap. A valid 16-digit number can even contain a valid 14 or 15-digit number. Your program must mask every digit.
I already wrote a test suite, so you can jump straight to the fun part: writing the algorithm. To participate:
mask.sh to call your program.run.sh.run.sh passes, post a link to your solution in the comments on our blog.Windows users should use Cygwin to run the tests. Please make it easy for others to check out and run your solution.
The first time you execute run.sh, you'll see a test failure:
$ ./run.sh
Running tests against mask.sh...
.X
Test #2 of 20 failed:
Description: valid 14-digit #
Input: 56613959932537\n
Expected result: XXXXXXXXXXXXXX\n
Actual result: 56613959932537\n
Modify mask.sh and make the tests pass. Line feeds delineate the test cases. If you pass a number on the command line, run.sh will repeat the test suite the specified number of times; this is useful for performance comparisons. The tests aren't set in stone—if you have an idea for improving the test suite, please submit a pull request.
This isn't a contest, but an innovative solution could score you interviews at Square. I'm primarily interested to see how different programming languages stack up with regard to readability and performance.
Once we have enough interesting submissions, I'll summarize the results in a followup blog post and open source our own Java-based implementation. In the mean time, if you enjoy working with talented people on challenging problems like this, email your résumé to luhnybin@squareup.com.
Good luck!
43 commits
Shell
100.0%