| 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 across all robot instances |
| 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 has no name yet |
| 51 | if self._name is None: |
| 52 | # Edge Case: All possible names have been used |
| 53 | if Robot._name_index >= len(Robot._all_names): |
| 54 | raise RuntimeError("No more names available") |
| 55 | |
| 56 | # Get the next available name |
| 57 | self._name = Robot._all_names[Robot._name_index] |
| 58 | Robot._name_index += 1 |
| 59 | Robot._used_names.add(self._name) |
| 60 | |
| 61 | return self._name |
| 62 | |
| 9 | 63 | def reset(self) -> None: |
| 10 | | pass |
| 64 | """ |
| 65 | Reset the robot to its factory settings. |
| 66 | |
| 67 | This wipes the robot's current name, making it available for reuse. |
| 68 | The next time the name is accessed, a new random name will be generated. |
| 69 | """ |
| 70 | # Edge Case: Robot has a name that needs to be released back to the pool |
| 71 | if self._name is not None: |
| 72 | Robot._used_names.discard(self._name) |
| 73 | self._name = None |
| 74 | |
| 75 | # Handled Edge Cases: Robot has no name yet, all possible names have been used, robot has a name that needs to be released back to the pool |