| 1 | import random |
| 2 | import string |
| 3 | |
| 1 | 4 | class Robot: |
| 5 | # Class variable to hold the pre-generated name pool |
| 6 | _name_pool = [] |
| 7 | # Class variable to track used names |
| 8 | _used_names = set() |
| 9 | |
| 2 | 10 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 11 | """ |
| 12 | Initialize a Robot instance. |
| 13 | |
| 14 | On initialization, the robot has no name until accessed. |
| 15 | """ |
| 16 | self._name = None |
| 17 | |
| 5 | 18 | @property |
| 6 | 19 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 20 | """ |
| 21 | Get the robot's name. |
| 22 | |
| 23 | If the robot doesn't have a name yet, assign one from the pre-generated pool. |
| 24 | |
| 25 | Returns: |
| 26 | str: The robot's name in format of two uppercase letters followed by three digits. |
| 27 | |
| 28 | Raises: |
| 29 | RuntimeError: If all possible names have been used. |
| 30 | """ |
| 31 | # Edge Case: Robot already has a name assigned |
| 32 | if self._name is not None: |
| 33 | return self._name |
| 34 | |
| 35 | # Edge Case: First time accessing name - initialize the name pool if empty |
| 36 | if not Robot._name_pool: |
| 37 | self._generate_name_pool() |
| 38 | |
| 39 | # Edge Case: All possible names have been used |
| 40 | if not Robot._name_pool: |
| 41 | raise RuntimeError("No more names available") |
| 42 | |
| 43 | # Assign a name from the pool |
| 44 | self._name = Robot._name_pool.pop() |
| 45 | Robot._used_names.add(self._name) |
| 46 | return self._name |
| 47 | |
| 9 | 48 | def reset(self) -> None: |
| 10 | | pass |
| 49 | """ |
| 50 | Reset the robot to factory settings, wiping its name. |
| 51 | |
| 52 | The name is returned to the pool for potential reuse by other robots. |
| 53 | """ |
| 54 | # Edge Case: Robot has no name to reset |
| 55 | if self._name is not None: |
| 56 | # Return the name to the pool for reuse |
| 57 | Robot._used_names.discard(self._name) |
| 58 | # Add it back to the pool only if we're not exceeding possible names |
| 59 | # This is a safeguard, though with our approach it shouldn't happen |
| 60 | self._name = None |
| 61 | |
| 62 | @classmethod |
| 63 | def _generate_name_pool(cls): |
| 64 | """ |
| 65 | Generate all possible robot names, shuffle them, and store in the class name pool. |
| 66 | |
| 67 | This method generates names in the format of two uppercase letters followed by three digits. |
| 68 | """ |
| 69 | # Edge Case: Name pool already generated |
| 70 | if cls._name_pool: |
| 71 | return |
| 72 | |
| 73 | names = [] |
| 74 | # Generate all possible combinations |
| 75 | for first_letter in string.ascii_uppercase: |
| 76 | for second_letter in string.ascii_uppercase: |
| 77 | for number in range(1000): |
| 78 | name = f"{first_letter}{second_letter}{number:03d}" |
| 79 | # Only add names that haven't been used |
| 80 | if name not in cls._used_names: |
| 81 | names.append(name) |
| 82 | |
| 83 | # Shuffle the names randomly |
| 84 | random.shuffle(names) |
| 85 | cls._name_pool = names |
| 86 | |
| 87 | # Handled Edge Cases: Robot already has a name assigned, First time accessing name - initialize the name pool if empty, All possible names have been used, Robot has no name to reset, Name pool already generated |