| 1 | 1 | class PhoneNumber: |
| 2 | 2 | def __init__(self, number): |
| 3 | | pass |
| 3 | # Remove all non-digit characters |
| 4 | cleaned = "" |
| 5 | for char in number: |
| 6 | if char.isdigit(): |
| 7 | cleaned += char |
| 8 | elif char in " .()-+": |
| 9 | # Punctuation is allowed but will be removed |
| 10 | continue |
| 11 | else: |
| 12 | # Letters or other special characters |
| 13 | if char.isalpha(): |
| 14 | raise ValueError("letters not permitted") |
| 15 | else: |
| 16 | raise ValueError("punctuations not permitted") |
| 17 | |
| 18 | # Edge Case: Empty input or no digits |
| 19 | if not cleaned: |
| 20 | raise ValueError("must not be fewer than 10 digits") |
| 21 | |
| 22 | # Edge Case: Number with less than 10 digits |
| 23 | if len(cleaned) < 10: |
| 24 | raise ValueError("must not be fewer than 10 digits") |
| 25 | |
| 26 | # Edge Case: Number with more than 11 digits |
| 27 | if len(cleaned) > 11: |
| 28 | raise ValueError("must not be greater than 11 digits") |
| 29 | |
| 30 | # Edge Case: Input starts with + but first digit is not 1 |
| 31 | if number.startswith('+') and cleaned and cleaned[0] != '1': |
| 32 | raise ValueError("must not be greater than 11 digits") |
| 33 | |
| 34 | # Edge Case: 11-digit number that doesn't start with 1 |
| 35 | if len(cleaned) == 11 and cleaned[0] != '1': |
| 36 | raise ValueError("11 digits must start with 1") |
| 37 | |
| 38 | # If 11 digits and starts with 1, remove the country code |
| 39 | if len(cleaned) == 11: |
| 40 | cleaned = cleaned[1:] |
| 41 | |
| 42 | # Edge Case: Area code starts with 0 |
| 43 | if cleaned[0] == '0': |
| 44 | raise ValueError("area code cannot start with zero") |
| 45 | |
| 46 | # Edge Case: Area code starts with 1 |
| 47 | if cleaned[0] == '1': |
| 48 | raise ValueError("area code cannot start with one") |
| 49 | |
| 50 | # Edge Case: Exchange code starts with 0 |
| 51 | if cleaned[3] == '0': |
| 52 | raise ValueError("exchange code cannot start with zero") |
| 53 | |
| 54 | # Edge Case: Exchange code starts with 1 |
| 55 | if cleaned[3] == '1': |
| 56 | raise ValueError("exchange code cannot start with one") |
| 57 | |
| 58 | self.number = cleaned |
| 59 | self.area_code = cleaned[:3] |
| 60 | |
| 61 | def pretty(self): |
| 62 | return f"({self.number[:3]})-{self.number[3:6]}-{self.number[6:]}" |
| 63 | |
| 64 | # Handled Edge Cases: Empty input, less than 10 digits, more than 11 digits, 11 digits not starting with 1, area code starting with 0 or 1, exchange code starting with 0 or 1, letters in input, invalid punctuation |