Finished
11class Luhn:
22 def __init__(self, card_num):
3 pass
3 self.card_num = card_num
44
55 def valid(self):
6 pass
6 # Edge Case: Strings of length 1 or less are not valid
7 if len(self.card_num) <= 1:
8 return False
9
10 # Remove spaces
11 cleaned = self.card_num.replace(" ", "")
12
13 # Edge Case: Strings of length 1 or less after space removal are not valid
14 if len(cleaned) <= 1:
15 return False
16
17 # Edge Case: All other non-digit characters are disallowed
18 if not cleaned.isdigit():
19 return False
20
21 total = 0
22 # Process from right to left, doubling every second digit
23 for i in range(len(cleaned)):
24 digit = int(cleaned[len(cleaned) - 1 - i])
25
26 # Double every second digit starting from the right
27 if i % 2 == 1:
28 digit *= 2
29 if digit > 9:
30 digit -= 9
31
32 total += digit
33
34 # Edge Case: Sum must be evenly divisible by 10
35 return total % 10 == 0
36
37# Handled Edge Cases: Strings with length 1 or less, Strings with only spaces, Strings with non-digit characters, Empty strings, Strings with leading/trailing spaces
Test NameStatus
test_a_simple_valid_sin_that_becomes_invalid_if_reversed
Pass
test_a_simple_valid_sin_that_remains_valid_if_reversed
Pass
test_a_single_zero_is_invalid
Pass
test_a_valid_canadian_sin
Pass
test_input_digit_9_is_correctly_converted_to_output_digit_9
Pass
test_invalid_canadian_sin
Pass
test_invalid_credit_card
Pass
test_invalid_long_number_with_a_remainder_divisible_by_5
Pass
test_invalid_long_number_with_an_even_remainder
Pass
test_is_valid_can_be_called_repeatedly
Pass
test_more_than_a_single_zero_is_valid
Pass
test_non_numeric_non_space_char_in_the_middle_with_a_sum_that_s_divisible_by_10_isn_t_allowed
Pass
test_single_digit_strings_can_not_be_valid
Pass
test_single_zero_with_space_is_invalid
Pass
test_using_ascii_value_for_doubled_non_digit_isn_t_allowed
Pass
test_using_ascii_value_for_non_doubled_non_digit_isn_t_allowed
Pass
test_valid_luhn_with_an_odd_number_of_digits_and_non_zero_first_digit
Pass
test_valid_number_with_an_even_number_of_digits
Pass
test_valid_number_with_an_odd_number_of_spaces
Pass
test_valid_strings_with_a_non_digit_added_at_the_end_become_invalid
Pass
test_valid_strings_with_punctuation_included_become_invalid
Pass
test_valid_strings_with_symbols_included_become_invalid
Pass
test_very_long_input_is_valid
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.