| 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 | Names are unique and randomly selected from a pre-generated pool. |
| 10 | """ |
| 11 | |
| 12 | # Class-level set to store all used names to ensure uniqueness |
| 13 | _used_names = set() |
| 14 | |
| 15 | # Pre-generate all possible valid names |
| 16 | _name_pool = [ |
| 17 | f"{letter1}{letter2}{digit1:03d}" |
| 18 | for letter1 in string.ascii_uppercase |
| 19 | for letter2 in string.ascii_uppercase |
| 20 | for digit1 in range(1000) |
| 21 | ] |
| 22 | |
| 23 | # Shuffle the name pool to ensure randomness |
| 24 | random.shuffle(_name_pool) |
| 25 | |
| 26 | # Class-level index to track the next available name |
| 27 | _next_index = 0 |
| 28 | |
| 2 | 29 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 30 | """ |
| 31 | Initialize a new Robot instance with no name. |
| 32 | The actual name is generated on first access. |
| 33 | """ |
| 34 | self._name = None |
| 35 | |
| 5 | 36 | @property |
| 6 | 37 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 38 | """ |
| 39 | Get the robot's name. |
| 40 | |
| 41 | If the robot doesn't have a name yet, generate one randomly from the pool. |
| 42 | |
| 43 | Returns: |
| 44 | str: The robot's name in format AA000. |
| 45 | |
| 46 | Raises: |
| 47 | RuntimeError: If all possible names have been used. |
| 48 | """ |
| 49 | # Edge Case: Robot already has a name, return it |
| 50 | if self._name is not None: |
| 51 | return self._name |
| 52 | |
| 53 | # Edge Case: All possible names have been used |
| 54 | if Robot._next_index >= len(Robot._name_pool): |
| 55 | raise RuntimeError("No more unique names available") |
| 56 | |
| 57 | # Get the next available name from the pool |
| 58 | self._name = Robot._name_pool[Robot._next_index] |
| 59 | Robot._next_index += 1 |
| 60 | Robot._used_names.add(self._name) |
| 61 | |
| 62 | return self._name |
| 63 | |
| 9 | 64 | def reset(self) -> None: |
| 10 | | pass |
| 65 | """ |
| 66 | Reset the robot to factory settings, wiping its name. |
| 67 | |
| 68 | The name is removed from the used names set and will be available |
| 69 | for future robots. |
| 70 | """ |
| 71 | # Edge Case: Robot has no name to reset |
| 72 | if self._name is None: |
| 73 | return |
| 74 | |
| 75 | # Remove the current name from used names |
| 76 | Robot._used_names.discard(self._name) |
| 77 | self._name = None |
| 78 | |
| 79 | # Handled Edge Cases: Robot already has a name, all possible names have been used, robot has no name to reset |