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