Coder Social home page Coder Social logo

credit_check's Introduction

Background: Luhn Algorithm

The Luhn algorithm is a check-summing algorithm best known for checking the validity of credit card numbers.

You can checkout the full description on Wikipedia: http://en.wikipedia.org/wiki/Luhn_algorithm

Description

(adapted from Wikipedia)

The formula verifies a number against its included check digit, which is usually appended to a partial account number to generate the full account number. This full account number must pass the following test:

  • from the rightmost digit, which is the check digit, moving left, double the value of every second digit
  • if product of this doubling operation is greater than 9 (e.g., 7 * 2 = 14), then sum the digits of the products (e.g., 10: 1 + 0 = 1, 14: 1 + 4 = 5).
  • take the sum of all the digits
  • if and only if the total modulo 10 is equal to 0 then the number is valid

Example

Validating an Account Number

Using 79927398713 as our sample input:

Account number:        7   9   9   2   7   3   9   8   7   1   3
2x every other digit:  7   18  9   4   7   6   9   16  7   2   3
Summed digits over 10: 7   9   9   4   7   6   9   7   7   2   3
Results summed:        7   9   9   4   7   6   9   7   7   2   3 = 70
Modulo Ten:            70 % 10 == 0

Since the summed results modulo 10 is zero, the account number is valid according to the algorithm.

Iteration 1 - The Luhn Algorithm

Start with this template and save it as credit_check.rb in your lib directory:

card_number = "4929735477250543"

 Your Luhn Algorithm Here

 Output
 If it is valid, print "The number is valid!"
 If it is invalid, print "The number is invalid!"

Sample Data

If helpful, you can use the following sample data:

  • Valid: 5541808923795240, 4024007136512380, 6011797668867828
  • Invalid: 5541801923795240, 4024007106512380, 6011797668868728

Iteration 2 - Github and Classes

  • Create a repository on Github and push your algorithm to it. From here on, all your code should be hosted on Github.

  • Create a class that responds to the following interaction pattern:

credit_check = CreditCheck.new
=> #<CreditCheck:0x00007fe82d2a8a38>
credit_check.valid_number?(5541808923795240)
=> true
credit_check.valid_number?(5541801923795240)
=> false
credit_check.validation_output(5541808923795240)
=> "The number 5541808923795240 is valid"
credit_check.validation_output(5541801923795240)
=> "The number 5541801923795240 is invalid"

note on interaction patterns: this means that you should be able to open a pry session, require the file with your class, for instance require './credit_check', and type in the lines of code above exactly and get the same output in your pry session.

Iteration 3 - Branching and Minitest

  • Create a branch in your local git repo called testing

  • Create a Minitest test that accurately covers the expected behavior of your CreditCheck class

  • Push the branch up to your Github repository and merge the branch using a Pull Request

Iteration 4 - Extensions

  • Create a command line interface that allows the user to validate a number

  • Add functionality to calculate the check sum digit.

  • Can you make it work for American Express numbers? 342804633855673 is valid but 342801633855673 is invalid

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.