| 1 | import random |
| 2 | import string |
| 3 | |
| 1 | 4 | class Robot: |
| 5 | """ |
| 6 | A class representing a robot with a randomly assigned 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 a new random name. |
| 10 | All names are unique across all robot instances. |
| 11 | """ |
| 12 | |
| 13 | # Class-level name pool to ensure uniqueness across all robot instances |
| 14 | _name_pool = None |
| 15 | |
| 2 | 16 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 17 | """ |
| 18 | Initialize a new Robot instance with no name. |
| 19 | The name will be generated when first accessed. |
| 20 | """ |
| 21 | self._assigned_name = None |
| 22 | |
| 23 | @classmethod |
| 24 | def _initialize_name_pool(cls): |
| 25 | """ |
| 26 | Initialize the pool of all possible valid names. |
| 27 | This is done only once when the first robot is created. |
| 28 | """ |
| 29 | # Edge Case: Name pool initialization |
| 30 | # This handles the initial setup of all possible robot names |
| 31 | cls._name_pool = [] |
| 32 | for first_letter in string.ascii_uppercase: |
| 33 | for second_letter in string.ascii_uppercase: |
| 34 | for number in range(1000): |
| 35 | cls._name_pool.append(f"{first_letter}{second_letter}{number:03d}") |
| 36 | random.shuffle(cls._name_pool) |
| 37 | |
| 5 | 38 | @property |
| 6 | 39 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 40 | """ |
| 41 | Get the robot's name. |
| 42 | |
| 43 | If the robot doesn't have a name yet, assign one randomly from the pool. |
| 44 | |
| 45 | Returns: |
| 46 | str: The robot's name in format AA000 |
| 47 | |
| 48 | Raises: |
| 49 | RuntimeError: If all possible names have been exhausted |
| 50 | """ |
| 51 | # Edge Case: First access to name - generate a new name |
| 52 | if self._assigned_name is None: |
| 53 | # Initialize the name pool if it hasn't been done yet |
| 54 | if Robot._name_pool is None: |
| 55 | Robot._initialize_name_pool() |
| 56 | |
| 57 | # Edge Case: Name pool exhaustion - no more unique names available |
| 58 | if not Robot._name_pool: |
| 59 | raise RuntimeError("No more unique names available") |
| 60 | |
| 61 | # Assign a name from the pool |
| 62 | self._assigned_name = Robot._name_pool.pop() |
| 63 | |
| 64 | return self._assigned_name |
| 65 | |
| 9 | 66 | def reset(self) -> None: |
| 10 | | pass |
| 67 | """ |
| 68 | Reset the robot to factory settings, wiping its name. |
| 69 | |
| 70 | The next time the name is accessed, a new random name will be generated. |
| 71 | The old name is returned to the pool to potentially be reused by other robots. |
| 72 | """ |
| 73 | # Edge Case: Resetting a robot that never had a name assigned |
| 74 | if self._assigned_name is not None: |
| 75 | # Return the name to the pool for potential reuse |
| 76 | # Edge Case: Handling name pool restoration during reset |
| 77 | if Robot._name_pool is not None: # Safety check |
| 78 | Robot._name_pool.append(self._assigned_name) |
| 79 | # Reshuffle to maintain randomness |
| 80 | random.shuffle(Robot._name_pool) |
| 81 | |
| 82 | # Clear the assigned name |
| 83 | self._assigned_name = None |
| 84 | |
| 85 | # Handled Edge Cases: First access to name, name pool initialization, name pool exhaustion, resetting robot without name, name pool restoration |