Skip to content

Commit

Permalink
Luhn Algorithm Completed
Browse files Browse the repository at this point in the history
  • Loading branch information
harens committed Jan 23, 2019
1 parent 8310faf commit 99b68c9
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 8 deletions.
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
<br>
*Checking digits with a digit!*

![Sample Parity](./art/parity.png) ![Sample UPC](./art/upc.png)

![Sample ISBN](./art/isbn.png) ![Sample Luhn](./art/luhn.png)

## Installation
```shell
pip install checkdigit
Expand All @@ -16,8 +20,20 @@ Or download the project [here](https://github.com/harens/checkdigit/archive/mast
## Features
- Add a parity digit to a string of binary
- ISBN
- Validate both ISBN-10 and ISBN-13 codes
- Determine missing digits
- Calculate check digits
- Validates both ISBN-10 and ISBN-13 Codes
- Determines Missing Digits
- Calculates Check Digits
- UPC
- Evaluates Check Digits
- Validates UPC Codes
- Luhn
- Validates Credit Cards, IMEI Numbers, and more!
- Determines Check Digits


## Tests
The test folder can be found here [here](https://github.com/harens/checkdigit/tree/master/tests)

You can run the tests by running `python tests.py`
## License
This project is licensed under the [GNU General Public License v3.0](https://github.com/harens/checkdigit/blob/master/LICENSE)
Binary file added art/isbn.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added art/luhn.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added art/parity.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added art/upc.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion checkdigit/isbn.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def isbn13calculate(data, function_name='isbn'):
if function_name == 'isbn':
mod_number = 0
else:
mod_number = 1
mod_number = 1 # Used for UPC
total_sum = 0
position_counter = 1 # 1 based indexing for data
for item in data:
Expand Down
25 changes: 25 additions & 0 deletions checkdigit/luhn.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,28 @@
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with checkdigit. If not, see <http://www.gnu.org/licenses/>.


def luhn_calculate(data):
position_counter = 1
total_sum = 0
for item in data[::-1]: # Reverses String
item = int(item)
if position_counter % 2 == 1:
add_value = item * 2
if add_value > 9:
for number in str(add_value): # Adds individual digits together
total_sum += int(number)
else:
total_sum += add_value
else:
total_sum += item
position_counter += 1
final_result = 10 - (total_sum % 10)
if final_result == 10:
return '0'
return str(final_result)


def luhn_validate(data):
return luhn_calculate(data[:-1]) == data[-1]
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

setup(
name="checkdigit",
version="0.0.3",
version="0.0.4",
description="Checking digits with a digit!",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
4 changes: 3 additions & 1 deletion tests/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ def test(function, value, function_name):
if 'parity' in function_name:
file_name = 'test_parity.py'
elif 'upc' in function_name:
file_name = 'test_upc'
file_name = 'test_upc.py'
elif 'luhn' in function_name:
file_name = 'test_luhn.py'
else:
file_name = 'test_isbn'
global total_tests
Expand Down
44 changes: 44 additions & 0 deletions tests/test_luhn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# /usr/bin/env python

# This file is part of checkdigit.

# checkdigit is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# checkdigit is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with checkdigit. If not, see <http://www.gnu.org/licenses/>.

import sys
import time
from config import test

sys.path.append("../") # Go back a directory
from checkdigit import luhn

start_time = time.time()

test(luhn.luhn_calculate('53251309870224'), '3', 'luhn_calculate(1)')
test(luhn.luhn_calculate('601195843045086'), '9', 'luhn_calculate(2)')
test(luhn.luhn_calculate('543669577125419'), '0', 'luhn_calculate(3)')
test(luhn.luhn_calculate('436076111511668'), '6', 'luhn_calculate(4)')
test(luhn.luhn_calculate('37266630277430'), '0', 'luhn_calculate(5)')
test(luhn.luhn_calculate('91497796683515'), '3', 'luhn_calculate(6)')
test(luhn.luhn_calculate('10408772972296'), '9', 'luhn_calculate(7)')

test(luhn.luhn_validate('541756116585277'), True, 'luhn_validate(1)')
test(luhn.luhn_validate('526012730485489'), True, 'luhn_validate(2)')
test(luhn.luhn_validate('515853022176176'), True, 'luhn_validate(3)')
test(luhn.luhn_validate('6011365035868968'), True, 'luhn_validate(4)')
test(luhn.luhn_validate('372098369216316'), True, 'luhn_validate(5)')
test(luhn.luhn_validate('4556098986775827'), True, 'luhn_validate(6)')

finish_time = time.time()


def luhn_time():
return finish_time - start_time
5 changes: 3 additions & 2 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

import test_isbn
import test_parity
import config
import test_upc
import test_luhn
import config

config.final_output(test_isbn.isbn_time() + test_parity.parity_time() + test_upc.upc_time())
config.final_output(test_isbn.isbn_time() + test_parity.parity_time() + test_upc.upc_time() + test_luhn.luhn_time())

0 comments on commit 99b68c9

Please sign in to comment.