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