| 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 | Each robot has a unique name among currently active robots. |
| 10 | """ |
| 11 | # Class-level set to track all currently used names |
| 12 | _used_names = set() |
| 13 | |
| 2 | 14 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 15 | """ |
| 16 | Initialize a new Robot instance with no name. |
| 17 | A name will be generated when first accessed. |
| 18 | """ |
| 19 | self._name = None |
| 20 | |
| 5 | 21 | @property |
| 6 | 22 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 23 | """ |
| 24 | Get the robot's name. If the robot doesn't have a name yet, |
| 25 | generate a new unique random name. |
| 26 | |
| 27 | Returns: |
| 28 | str: The robot's name in format like 'AB123' |
| 29 | |
| 30 | Raises: |
| 31 | RuntimeError: If all possible names are exhausted |
| 32 | """ |
| 33 | # Edge Case: Robot has no name yet, generate one |
| 34 | if self._name is None: |
| 35 | self._generate_unique_name() |
| 36 | return self._name |
| 37 | |
| 9 | 38 | def reset(self) -> None: |
| 10 | | pass |
| 39 | """ |
| 40 | Reset the robot to factory settings by wiping its name. |
| 41 | The old name becomes available for reuse by other robots. |
| 42 | """ |
| 43 | # Edge Case: Wiping a robot's name - mark the name as available for reuse |
| 44 | if self._name is not None: |
| 45 | Robot._used_names.discard(self._name) |
| 46 | self._name = None |
| 47 | |
| 48 | def _generate_unique_name(self) -> None: |
| 49 | """ |
| 50 | Generate a unique random name for the robot. |
| 51 | |
| 52 | The name format is two uppercase letters followed by three digits. |
| 53 | This method ensures the generated name is unique among currently active robots. |
| 54 | |
| 55 | Raises: |
| 56 | RuntimeError: If all possible names are exhausted |
| 57 | """ |
| 58 | # Edge Case: All possible names are exhausted (676,000 combinations) |
| 59 | max_names = 26 * 26 * 10 * 10 * 10 |
| 60 | if len(Robot._used_names) >= max_names: |
| 61 | raise RuntimeError("All possible robot names have been exhausted") |
| 62 | |
| 63 | # Edge Case: Handle potential name collision by retrying until unique name found |
| 64 | max_attempts = 100 # Prevent infinite loop in case of high collision rate |
| 65 | attempts = 0 |
| 66 | |
| 67 | while attempts < max_attempts: |
| 68 | # Generate two random uppercase letters |
| 69 | letters = ''.join(random.choices(string.ascii_uppercase, k=2)) |
| 70 | # Generate three random digits |
| 71 | digits = ''.join(random.choices(string.digits, k=3)) |
| 72 | # Combine to form the name |
| 73 | new_name = letters + digits |
| 74 | |
| 75 | # Check if this name is already in use |
| 76 | if new_name not in Robot._used_names: |
| 77 | # Name is unique, assign it to this robot |
| 78 | Robot._used_names.add(new_name) |
| 79 | self._name = new_name |
| 80 | return |
| 81 | |
| 82 | attempts += 1 |
| 83 | |
| 84 | # Edge Case: Failed to generate unique name within reasonable attempts |
| 85 | raise RuntimeError("Unable to generate unique robot name after maximum attempts") |
| 86 | |
| 87 | # Handled Edge Cases: Robot has no name yet, generate one; Handle potential name collision by retrying until unique name found; Wiping a robot's name - mark the name as available for reuse; All possible names are exhausted (676,000 combinations); Failed to generate unique name within reasonable attempts |