P-AI-R Programming: How AI Tools Like GitHub Copilot and ChatGPT Can Radically Transform Your Development Workflow by Michael D Callaghan

P-AI-R Programming: How AI Tools Like GitHub Copilot and ChatGPT Can Radically Transform Your Development Workflow by Michael D Callaghan

Author:Michael D Callaghan
Language: eng
Format: epub
Tags: AI, Software Development, ChatGPT, GitHub Copilot, Copilot, Machine Learning, Code Generation
Publisher: Michael Callaghan
Published: 2024-01-25T00:00:00+00:00


Invalid CC#

4321 5678 4321 5678

6011 2222 3333 4444

4444 3333 2222 1111

5432 1234 5678 9999

378282246310005 (Valid Amex)

That last one is a false negative. Apparently, the regex cannot match American Express card numbers.

One last thought I had was whether or not Copilot knows what the Lunh Algorithm is. The JavaScript is available on the developer page I linked above.

I got this response when I typed my comment, which is not what I expected:

// Validate the credit card number using the Lunh Algorithm

private creditCardLuhnRegex = /^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|6(?:011|5[0-9][0-9])[0-9]{12}|(?:2131|1800|35\d{3})\d{11})$/;

It doesn’t actually appear to implement the algorithm, but it does match the Amex number that the first regex missed.

Before leaving this section, I wondered if a slight change to my comment might make it work.

// Function to Validate the credit card number using the Lunh Algorithm

private validateCreditCardNumber(cardNumber: string): boolean {

let sum = 0;

let shouldDouble = false;

for (let i = cardNumber.length - 1; i >= 0; i—) {

let digit = parseInt(cardNumber.charAt(i), 10);

if (shouldDouble) {

if ((digit *= 2) > 9) digit -= 9;

}

sum += digit;

shouldDouble = !shouldDouble;

}

return sum % 10 === 0;

} // End of validateCreditCardNumber

I didn’t run it to test it out, but that is a lot closer to what I was expecting. Feel free to give it a try.

I suspected that ChatGPT would be better for this sort of thing, so I gave it a similar prompt to see what it would come up with.

Implement a function to validate a credit card number using the Lunh Algorithm

Sure, here's a Python function that uses the Luhn Algorithm to validate a credit card number:

def validate_credit_card_number(card_number):

# Reverse the credit card number and convert each digit to an integer

digits = [int(x) for x in str(card_number)][::-1]

# Double the value of every second digit

doubled_digits = []

for i, digit in enumerate(digits):

if i % 2 == 1:

doubled_digits.append(digit * 2)

else:

doubled_digits.append(digit)

# Add the digits of the doubled values and the undoubled digits

total = 0

for digit in doubled_digits:

if digit > 9:

total += digit - 9

else:

total += digit

# If the total is a multiple of 10, the card number is valid

return total % 10 == 0

Does it work? I don’t really know. Honestly, I didn’t test it. I don’t speak Python, but it did give me a glimpse at some of the languages the ChatGPT (and presumably Copilot) knows. The JavaScript I got from Copilot looks different from the JavaScript code at the website.

If you decide to test either version, let me know if it works.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.