| 1 | import random |
| 2 | import string |
| 3 | |
| 1 | 4 | class Robot: |
| 5 | """ |
| 6 | A class representing a robot with a randomly assigned name. |
| 7 | |
| 8 | The robot's name is generated in the format of two uppercase letters followed by three digits. |
| 9 | Each robot has a unique name, and the name can be reset to a new random one. |
| 10 | """ |
| 11 | |
| 12 | # Class-level set to track all names that have been assigned |
| 13 | _used_names = set() |
| 14 | # Class-level list of all possible names, shuffled once at class definition time |
| 15 | _name_pool = [] |
| 16 | |
| 17 | # Generate all possible names once and shuffle them |
| 18 | for letter1 in string.ascii_uppercase: |
| 19 | for letter2 in string.ascii_uppercase: |
| 20 | for digit1 in range(10): |
| 21 | for digit2 in range(10): |
| 22 | for digit3 in range(10): |
| 23 | _name_pool.append(f"{letter1}{letter2}{digit1}{digit2}{digit3}") |
| 24 | |
| 25 | # Shuffle the name pool to ensure randomness |
| 26 | random.shuffle(_name_pool) |
| 27 | # Index to track the next available name from the pool |
| 28 | _pool_index = 0 |
| 29 | |
| 2 | 30 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 31 | """ |
| 32 | Initialize a new Robot instance with no name. |
| 33 | The actual name is assigned on first access. |
| 34 | """ |
| 35 | self._name = None |
| 36 | |
| 5 | 37 | @property |
| 6 | 38 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 39 | """ |
| 40 | Get the robot's name. If the robot doesn't have a name yet, |
| 41 | assign one from the pre-generated name pool. |
| 42 | |
| 43 | Returns: |
| 44 | str: The robot's name in format AA000. |
| 45 | """ |
| 46 | # Edge Case: Robot has no name yet, assign one from the pool |
| 47 | if self._name is None: |
| 48 | # Edge Case: All possible names have been used |
| 49 | if Robot._pool_index >= len(Robot._name_pool): |
| 50 | raise RuntimeError("No more unique names available") |
| 51 | |
| 52 | self._name = Robot._name_pool[Robot._pool_index] |
| 53 | Robot._used_names.add(self._name) |
| 54 | Robot._pool_index += 1 |
| 55 | |
| 56 | return self._name |
| 57 | |
| 9 | 58 | def reset(self) -> None: |
| 10 | | pass |
| 59 | """ |
| 60 | Reset the robot to factory settings, wiping its name. |
| 61 | The next access to the name property will generate a new random name. |
| 62 | """ |
| 63 | # Edge Case: Robot had a name, remove it from the used names set |
| 64 | if self._name is not None: |
| 65 | Robot._used_names.discard(self._name) |
| 66 | self._name = None |
| 67 | |
| 68 | # Handled Edge Cases: Robot has no name yet; All possible names have been used; Robot had a name to remove |