| 1 | import random |
| 2 | import string |
| 3 | |
| 1 | 4 | class Robot: |
| 5 | """ |
| 6 | A class representing a robot with a randomly generated name. |
| 7 | |
| 8 | The robot's name consists of two uppercase letters followed by three digits. |
| 9 | The name is generated when first accessed and can be reset to generate a new name. |
| 10 | """ |
| 11 | |
| 12 | # Class variable to keep track of all used names to ensure uniqueness |
| 13 | _used_names = set() |
| 14 | |
| 2 | 15 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 16 | """ |
| 17 | Initialize a Robot instance with no name. |
| 18 | The name will be generated when first accessed. |
| 19 | """ |
| 20 | self._name = None |
| 21 | |
| 5 | 22 | @property |
| 6 | 23 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 24 | """ |
| 25 | Get the robot's name. If the robot doesn't have a name yet, |
| 26 | generate a unique random name in the format of two uppercase letters |
| 27 | followed by three digits. |
| 28 | |
| 29 | Returns: |
| 30 | str: The robot's name |
| 31 | """ |
| 32 | # Edge Case: Robot has no name yet, generate a new one |
| 33 | if self._name is None: |
| 34 | self._generate_unique_name() |
| 35 | return self._name |
| 36 | |
| 9 | 37 | def reset(self) -> None: |
| 10 | | pass |
| 38 | """ |
| 39 | Reset the robot to its factory settings, wiping its name. |
| 40 | The next time the name is accessed, a new random name will be generated. |
| 41 | """ |
| 42 | # Edge Case: Robot has a name that needs to be removed from used names |
| 43 | if self._name is not None: |
| 44 | Robot._used_names.discard(self._name) |
| 45 | self._name = None |
| 46 | |
| 47 | def _generate_unique_name(self) -> None: |
| 48 | """ |
| 49 | Generate a unique random name for the robot. |
| 50 | The name format is two uppercase letters followed by three digits. |
| 51 | """ |
| 52 | # Edge Case: All possible names are used (very unlikely but theoretically possible) |
| 53 | max_attempts = 1000 # Prevent infinite loop in case of issues |
| 54 | attempts = 0 |
| 55 | |
| 56 | while attempts < max_attempts: |
| 57 | # Generate two random uppercase letters |
| 58 | letters = ''.join(random.choices(string.ascii_uppercase, k=2)) |
| 59 | # Generate three random digits |
| 60 | digits = ''.join(random.choices(string.digits, k=3)) |
| 61 | # Combine to form the name |
| 62 | new_name = letters + digits |
| 63 | |
| 64 | # Check if this name is unique |
| 65 | if new_name not in Robot._used_names: |
| 66 | Robot._used_names.add(new_name) |
| 67 | self._name = new_name |
| 68 | return |
| 69 | |
| 70 | attempts += 1 |
| 71 | |
| 72 | # This should never happen given the large number of possible names (26*26*10*10*10 = 676,000) |
| 73 | raise RuntimeError("Unable to generate a unique name after maximum attempts") |
| 74 | |
| 75 | # Handled Edge Cases: Robot has no name yet, Robot has a name that needs to be removed from used names, All possible names are used |