| 1 | import random |
| 2 | import string |
| 3 | |
| 1 | 4 | class Robot: |
| 5 | # Class variable to hold the pool of names and track used names |
| 6 | _name_pool = [] |
| 7 | _used_names = set() |
| 8 | |
| 2 | 9 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 10 | """ |
| 11 | Initialize a Robot instance. |
| 12 | |
| 13 | On initialization, the robot has no name until accessed. |
| 14 | """ |
| 15 | self._name = None |
| 16 | |
| 5 | 17 | @property |
| 6 | 18 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 19 | """ |
| 20 | Get the robot's name. If the robot doesn't have a name yet, |
| 21 | assign one from the pre-generated name pool. |
| 22 | |
| 23 | Returns: |
| 24 | str: The robot's name in format of two uppercase letters followed by three digits. |
| 25 | |
| 26 | Raises: |
| 27 | RuntimeError: If all possible names have been used. |
| 28 | """ |
| 29 | # Edge Case: Robot already has a name assigned |
| 30 | if self._name is not None: |
| 31 | return self._name |
| 32 | |
| 33 | # Edge Case: First access to name - need to generate the name pool if empty |
| 34 | if not Robot._name_pool: |
| 35 | self._generate_name_pool() |
| 36 | |
| 37 | # Edge Case: All possible names have been used |
| 38 | if not Robot._name_pool: |
| 39 | raise RuntimeError("No more names available") |
| 40 | |
| 41 | # Assign a name from the pool |
| 42 | self._name = Robot._name_pool.pop() |
| 43 | Robot._used_names.add(self._name) |
| 44 | return self._name |
| 45 | |
| 9 | 46 | def reset(self) -> None: |
| 10 | | pass |
| 47 | """ |
| 48 | Reset the robot to factory settings, wiping its name. |
| 49 | The name is returned to the pool for potential reuse. |
| 50 | """ |
| 51 | # Edge Case: Robot has no name to reset |
| 52 | if self._name is None: |
| 53 | return |
| 54 | |
| 55 | # Return the name to the pool |
| 56 | Robot._used_names.discard(self._name) |
| 57 | # We don't actually put it back in the pool since we want unique names per problem requirements |
| 58 | # But we do remove it from used names so it could theoretically be reused if we reinitialize everything |
| 59 | self._name = None |
| 60 | |
| 61 | @classmethod |
| 62 | def _generate_name_pool(cls) -> None: |
| 63 | """ |
| 64 | Generate all possible valid names, shuffle them, and store them in the pool. |
| 65 | """ |
| 66 | # Edge Case: Name pool already exists |
| 67 | if cls._name_pool: |
| 68 | return |
| 69 | |
| 70 | # Generate all possible names |
| 71 | names = [] |
| 72 | for letter1 in string.ascii_uppercase: |
| 73 | for letter2 in string.ascii_uppercase: |
| 74 | for digit1 in string.digits: |
| 75 | for digit2 in string.digits: |
| 76 | for digit3 in string.digits: |
| 77 | name = f"{letter1}{letter2}{digit1}{digit2}{digit3}" |
| 78 | # Only add names that haven't been used |
| 79 | if name not in cls._used_names: |
| 80 | names.append(name) |
| 81 | |
| 82 | # Shuffle the names randomly |
| 83 | random.shuffle(names) |
| 84 | cls._name_pool = names |
| 85 | |
| 86 | # Handled Edge Cases: Robot already has a name assigned, first access to name requiring pool generation, all possible names used, robot has no name to reset, name pool already exists |