Created
November 2, 2019 04:22
-
-
Save dzfranklin/1cf0c7cefccd78bcf845ce28891d6c01 to your computer and use it in GitHub Desktop.
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
"""Computes if a credit card number is valid. | |
Daniel Franklin <[email protected]> | |
HW due 15 Nov 2019 | |
""" | |
def normalize_input(input_str): | |
"""Normalize an input string into an easier to process format. | |
:param input_str: A string of user input in form "xxxx-xxxx-xxxx-xxxx" | |
:return list[int]: The cc number as a list of digits | |
""" | |
return [int(n) for n in input_str.replace("-", "")] | |
def validate_card(num): | |
"""Validate if a number is a valid credit card number. | |
:param num: The number to check as a list[int] | |
:return bool: If the number is valid | |
""" | |
requirements = [ | |
num[0] == 4, | |
num[4 - 1] == num[5 - 1] + 1, | |
num[1 - 1] * num[5 - 1] * num[9 - 1] == 24, | |
sum(num) % 4 == 0, | |
sum(num[:4]) == sum(num[-4:]) - 1, | |
(num[1 - 1] * 10 + num[2 - 1]) + (num[7 - 1] * 10 + num[8 - 1]) == 100 | |
] | |
return not (False in requirements) | |
def main(card_to_test): | |
return validate_card(normalize_input(card_to_test)) | |
def test(): | |
"""Check the program against test cases. | |
Throws AssertionError if a test case fails. | |
""" | |
tests = [ | |
["4132-1459-6972-3341", True], | |
["4442-1056-6063-4434", True], | |
["4104-3059-2802-1234", True], | |
["4444-3333-2222-2111", False], | |
["5105-1051-0510-5100", False] | |
] | |
for (input_str, expected_out) in tests: | |
print(f"Testing that \"{input_str}\" is a valid card number") | |
assert(main(input_str) == expected_out) | |
if __name__ == '__main__': | |
card_to_test = input("Card to test: ") | |
if main(card_to_test): | |
result_as_str = "" | |
else: | |
result_as_str = "NOT " | |
print(f"The credit card number {card_to_test} is {result_as_str}valid") |
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
from random import randint | |
from credit_card import validate_card # my checking function | |
NUMBER_OF_CARDS_TO_FIND = 2 | |
def pretty_format_card(nums): | |
"""Format a list of card digits into a the form xxxx-xxxx-xxxx-xxxx.""" | |
return "-".join( | |
"".join([str(num) for num in group]) | |
for group in [nums[:4], nums[4:8], nums[8:12], nums[12:]]) | |
iterations = 0 | |
found_cards = [] | |
while True: | |
# every 1,000,000th time this loop runs, print how many times it has run | |
if iterations % 10**6 == 0: | |
print(iterations) | |
# create a list of the number 4 followed by 15 random numbers | |
nums = [4] | |
while(len(nums) < 16): | |
nums.append(randint(0, 9)) | |
if validate_card(nums): | |
found_card = pretty_format_card(nums) | |
print(f"Found result: {found_card}") | |
found_cards.append(found_card) | |
if len(found_cards) >= NUMBER_OF_CARDS_TO_FIND: | |
break | |
iterations += 1 | |
print("\n\n") | |
for card in found_cards: | |
print(card) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment