| 3 | 3 | area_code: str # XXX |
| 4 | 4 | |
| 5 | 5 | def __init__(self, number: str): |
| 6 | | pass |
| 7 | | |
| 6 | # Edge Case: Empty or null input strings |
| 7 | if not number: |
| 8 | raise ValueError("must not be fewer than 10 digits") |
| 9 | |
| 10 | # Remove all non-digit characters |
| 11 | cleaned = "" |
| 12 | for char in number: |
| 13 | if char.isdigit(): |
| 14 | cleaned += char |
| 15 | elif char in " .()-+": |
| 16 | # Edge Case: Phone numbers with invalid punctuation |
| 17 | continue |
| 18 | else: |
| 19 | # Edge Case: Phone numbers containing letters |
| 20 | if char.isalpha(): |
| 21 | raise ValueError("letters not permitted") |
| 22 | # Edge Case: Phone numbers with special characters other than allowed punctuation |
| 23 | else: |
| 24 | raise ValueError("punctuations not permitted") |
| 25 | |
| 26 | # Edge Case: Phone numbers with incorrect digit count (too few) |
| 27 | if len(cleaned) < 10: |
| 28 | raise ValueError("must not be fewer than 10 digits") |
| 29 | |
| 30 | # Edge Case: Phone numbers with incorrect digit count (too many) |
| 31 | if len(cleaned) > 11: |
| 32 | raise ValueError("must not be greater than 11 digits") |
| 33 | |
| 34 | # Edge Case: Phone numbers with 11 digits, but starts with a number other than 1 |
| 35 | if len(cleaned) == 11: |
| 36 | if cleaned[0] != '1': |
| 37 | raise ValueError("11 digits must start with 1") |
| 38 | cleaned = cleaned[1:] |
| 39 | |
| 40 | # Edge Case: Area codes starting with 0 |
| 41 | if cleaned[0] == '0': |
| 42 | raise ValueError("area code cannot start with zero") |
| 43 | |
| 44 | # Edge Case: Area codes starting with 1 |
| 45 | if cleaned[0] == '1': |
| 46 | raise ValueError("area code cannot start with one") |
| 47 | |
| 48 | # Edge Case: Exchange codes starting with 0 |
| 49 | if cleaned[3] == '0': |
| 50 | raise ValueError("exchange code cannot start with zero") |
| 51 | |
| 52 | # Edge Case: Exchange codes starting with 1 |
| 53 | if cleaned[3] == '1': |
| 54 | raise ValueError("exchange code cannot start with one") |
| 55 | |
| 56 | self.number = cleaned |
| 57 | self.area_code = cleaned[:3] |
| 58 | |
| 8 | 59 | def pretty(self) -> str: # (XXX)-XXX-XXXX |
| 9 | | pass |
| 60 | return f"({self.number[:3]})-{self.number[3:6]}-{self.number[6:]}" |
| 61 | |
| 62 | # Handled Edge Cases: Empty or null input strings, Phone numbers with invalid punctuation, Phone numbers containing letters, Phone numbers with special characters other than allowed punctuation, Phone numbers with incorrect digit count (too few), Phone numbers with incorrect digit count (too many), Phone numbers with 11 digits but starts with a number other than 1, Area codes starting with 0, Area codes starting with 1, Exchange codes starting with 0, Exchange codes starting with 1 |