-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔧 Refactor DIFF format and add credit card gen tests
- Loading branch information
Showing
11 changed files
with
267 additions
and
28 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Function to generate a credit card number | ||
function generateCreditCardNumber() { | ||
// Define the prefix for Visa cards | ||
const prefix = "400000" | ||
// Define the total length of the credit card number | ||
const length = 16 | ||
// Initialize the card number with the prefix | ||
let cardNumber = prefix | ||
|
||
// Loop until the card number reaches the desired length minus the check digit | ||
while (cardNumber.length < length - 1) { | ||
// Append a random digit to the card number | ||
cardNumber += Math.floor(Math.random() * 10).toString() | ||
} | ||
|
||
// Append the check digit to the card number | ||
cardNumber += getCheckDigit(cardNumber) | ||
// Return the complete card number | ||
return cardNumber | ||
} | ||
|
||
// Function to calculate the check digit using the Luhn algorithm | ||
function getCheckDigit(cardNumber) { | ||
// Initialize the sum to 0 | ||
let sum = 0 | ||
// Flag to determine whether to double the digit or not | ||
let shouldDouble = true | ||
|
||
// Loop through the card number digits from right to left | ||
for (let i = cardNumber.length - 1; i >= 0; i--) { | ||
// Parse the current digit | ||
let digit = parseInt(cardNumber.charAt(i)) | ||
|
||
// If the flag is set, double the digit | ||
if (shouldDouble) { | ||
digit *= 2 | ||
// If the doubled digit is greater than 9, subtract 9 | ||
if (digit > 9) { | ||
digit -= 9 | ||
} | ||
} | ||
|
||
// Add the digit to the sum | ||
sum += digit | ||
// Toggle the flag for the next digit | ||
shouldDouble = !shouldDouble | ||
} | ||
|
||
// Calculate the check digit | ||
const checkDigit = newFunction(sum) | ||
// Return the check digit as a string | ||
return checkDigit.toString() | ||
} | ||
|
||
function newFunction(sum) { | ||
// TODO | ||
return 0 | ||
// return (10 - (sum % 10)) % 10; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Function to generate a credit card number | ||
def generate_credit_card_number(): | ||
# Define the prefix for Visa cards | ||
prefix = "400000" | ||
# Define the total length of the credit card number | ||
length = 16 | ||
# Initialize the card number with the prefix | ||
card_number = prefix | ||
|
||
# Loop until the card number reaches the desired length minus the check digit | ||
while len(card_number) < length - 1: | ||
# Append a random digit to the card number | ||
card_number += str(random.randint(0, 9)) | ||
|
||
# Append the check digit to the card number | ||
card_number += get_check_digit(card_number) | ||
# Return the complete card number | ||
return card_number | ||
|
||
# Function to calculate the check digit using the Luhn algorithm | ||
def get_check_digit(card_number): | ||
# Initialize the sum to 0 | ||
sum = 0 | ||
# Flag to determine whether to double the digit or not | ||
should_double = True | ||
|
||
# Loop through the card number digits from right to left | ||
for digit in reversed(card_number): | ||
digit = int(digit) | ||
|
||
# If the flag is set, double the digit | ||
if should_double: | ||
digit *= 2 | ||
# If the doubled digit is greater than 9, subtract 9 | ||
if digit > 9: | ||
digit -= 9 | ||
|
||
# Add the digit to the sum | ||
sum += digit | ||
# Toggle the flag for the next digit | ||
should_double = not should_double | ||
|
||
# Calculate the check digit | ||
check_digit = new_function(sum) | ||
# Return the check digit as a string | ||
return str(check_digit) | ||
|
||
def new_function(sum): | ||
# TODO | ||
return 0 | ||
# return (10 - (sum % 10)) % 10; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Function to generate a credit card number | ||
function generateCreditCardNumber(): string { | ||
// Define the prefix for Visa cards | ||
const prefix = "400000" | ||
// Define the total length of the credit card number | ||
const length = 16 | ||
// Initialize the card number with the prefix | ||
let cardNumber = prefix | ||
|
||
// Loop until the card number reaches the desired length minus the check digit | ||
while (cardNumber.length < length - 1) { | ||
// Append a random digit to the card number | ||
cardNumber += Math.floor(Math.random() * 10).toString() | ||
} | ||
|
||
// Append the check digit to the card number | ||
cardNumber += getCheckDigit(cardNumber) | ||
// Return the complete card number | ||
return cardNumber | ||
} | ||
|
||
// Function to calculate the check digit using the Luhn algorithm | ||
function getCheckDigit(cardNumber: string): string { | ||
// Initialize the sum to 0 | ||
let sum = 0 | ||
// Flag to determine whether to double the digit or not | ||
let shouldDouble = true | ||
|
||
// Loop through the card number digits from right to left | ||
for (let i = cardNumber.length - 1; i >= 0; i--) { | ||
// Parse the current digit | ||
let digit = parseInt(cardNumber.charAt(i)) | ||
|
||
// If the flag is set, double the digit | ||
if (shouldDouble) { | ||
digit *= 2 | ||
// If the doubled digit is greater than 9, subtract 9 | ||
if (digit > 9) { | ||
digit -= 9 | ||
} | ||
} | ||
|
||
// Add the digit to the sum | ||
sum += digit | ||
// Toggle the flag for the next digit | ||
shouldDouble = !shouldDouble | ||
} | ||
|
||
// Calculate the check digit | ||
const checkDigit = newFunction(sum) | ||
// Return the check digit as a string | ||
return checkDigit.toString() | ||
} | ||
|
||
function newFunction(sum: number) { | ||
// TODO | ||
return 0 | ||
// return (10 - (sum % 10)) % 10; | ||
} |
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
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
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
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
Oops, something went wrong.