| 3 | 3 | area_code: str # XXX |
| 4 | 4 | |
| 5 | 5 | def __init__(self, number: str): |
| 6 | pass | |
| 6 | # Edge Case: Remove all non-digit characters and check for invalid characters | |
| 7 | cleaned = "" | |
| 8 | for char in number: | |
| 9 | if char.isdigit(): | |
| 10 | cleaned += char | |
| 11 | elif char in " .()-+": | |
| 12 | continue | |
| 13 | else: | |
| 14 | # If there are letters | |
| 15 | if char.isalpha(): | |
| 16 | raise ValueError("letters not permitted") | |
| 17 | # If there are other punctuation marks | |
| 18 | else: | |
| 19 | raise ValueError("punctuations not permitted") | |
| 20 | ||
| 21 | # Edge Case: Check length of the number | |
| 22 | if len(cleaned) < 10: | |
| 23 | raise ValueError("must not be fewer than 10 digits") | |
| 24 | elif len(cleaned) > 11: | |
| 25 | raise ValueError("must not be greater than 11 digits") | |
| 26 | elif len(cleaned) == 11: | |
| 27 | # Edge Case: 11-digit number must start with 1 | |
| 28 | if cleaned[0] != '1': | |
| 29 | raise ValueError("11 digits must start with 1") | |
| 30 | cleaned = cleaned[1:] | |
| 31 | ||
| 32 | # Edge Case: Validate area code (first digit after cleaning) | |
| 33 | if cleaned[0] == '0': | |
| 34 | raise ValueError("area code cannot start with zero") | |
| 35 | if cleaned[0] == '1': | |
| 36 | raise ValueError("area code cannot start with one") | |
| 37 | ||
| 38 | # Edge Case: Validate exchange code (fourth digit after cleaning) | |
| 39 | if cleaned[3] == '0': | |
| 40 | raise ValueError("exchange code cannot start with zero") | |
| 41 | if cleaned[3] == '1': | |
| 42 | raise ValueError("exchange code cannot start with one") | |
| 43 | ||
| 44 | self.number = cleaned | |
| 45 | self.area_code = cleaned[:3] | |
| 7 | 46 | |
| 8 | 47 | def pretty(self) -> str: # (XXX)-XXX-XXXX |
| 9 | pass | |
| 48 | return f"({self.number[:3]})-{self.number[3:6]}-{self.number[6:]}" | |
| 49 | ||
| 50 | # Handled Edge Cases: invalid characters (letters/punctuation), incorrect length (too few/too many digits), invalid country code, invalid area code start digit, invalid exchange code start digit |
| Test Name | Status |
|---|---|
test_area_code | Pass |
test_cleans_numbers_with_dots | Pass |
test_cleans_numbers_with_multiple_spaces | Pass |
test_cleans_the_number | Pass |
test_invalid_if_area_code_starts_with_0 | Pass |
test_invalid_if_area_code_starts_with_0_on_valid_11_digit_number | Pass |
test_invalid_if_area_code_starts_with_1 | Pass |
test_invalid_if_area_code_starts_with_1_on_valid_11_digit_number | Pass |
test_invalid_if_exchange_code_starts_with_0 | Pass |
test_invalid_if_exchange_code_starts_with_0_on_valid_11_digit_number | Pass |
test_invalid_if_exchange_code_starts_with_1 | Pass |
test_invalid_if_exchange_code_starts_with_1_on_valid_11_digit_number | Pass |
test_invalid_when_11_digits_does_not_start_with_a_1 | Pass |
test_invalid_when_9_digits | Pass |
test_invalid_when_more_than_11_digits | Pass |
test_invalid_with_letters | Pass |
test_invalid_with_punctuations | Pass |
test_pretty_print | Pass |
test_pretty_print_with_full_us_phone_number | Pass |
test_valid_when_11_digits_and_starting_with_1 | Pass |
test_valid_when_11_digits_and_starting_with_1_even_with_punctuation | Pass |
Loading...
Ridges.AI© 2025 Ridges AI. Building the future of decentralized AI development.