| 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 |
| 9 | followed by three digits (e.g., RX837 or BC811). Each robot has a unique name. |
| 10 | |
| 11 | Attributes: |
| 12 | _name (str): The robot's name, generated on first access. |
| 13 | """ |
| 14 | |
| 15 | # Class variable to hold the pool of all possible names |
| 16 | _name_pool = [] |
| 17 | |
| 2 | 18 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 19 | """ |
| 20 | Initializes a new Robot instance. |
| 21 | |
| 22 | The robot starts without a name. A name will be assigned when first accessed. |
| 23 | """ |
| 24 | self._name = None |
| 25 | |
| 5 | 26 | @property |
| 6 | 27 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 28 | """ |
| 29 | Gets the robot's name. |
| 30 | |
| 31 | If the robot doesn't have a name yet, one is generated from the pre-generated pool. |
| 32 | |
| 33 | Returns: |
| 34 | str: The robot's name in the format of two uppercase letters followed by three digits. |
| 35 | |
| 36 | Raises: |
| 37 | Exception: If all possible names have been exhausted. |
| 38 | """ |
| 39 | # Edge Case: Robot's name has not been assigned yet |
| 40 | if self._name is None: |
| 41 | # Edge Case: Name pool has not been initialized |
| 42 | if not Robot._name_pool: |
| 43 | # Generate all possible names |
| 44 | letters = string.ascii_uppercase |
| 45 | digits = string.digits |
| 46 | all_names = [l1 + l2 + d1 + d2 + d3 |
| 47 | for l1 in letters |
| 48 | for l2 in letters |
| 49 | for d1 in digits |
| 50 | for d2 in digits |
| 51 | for d3 in digits] |
| 52 | |
| 53 | # Shuffle the names randomly |
| 54 | random.shuffle(all_names) |
| 55 | Robot._name_pool = all_names |
| 56 | |
| 57 | # Edge Case: All possible names have been used |
| 58 | if not Robot._name_pool: |
| 59 | raise Exception("All possible robot names have been exhausted.") |
| 60 | |
| 61 | # Assign the next name from the pool |
| 62 | self._name = Robot._name_pool.pop() |
| 63 | |
| 64 | return self._name |
| 65 | |
| 9 | 66 | def reset(self) -> None: |
| 10 | | pass |
| 67 | """ |
| 68 | Resets the robot to its factory settings, wiping its name. |
| 69 | |
| 70 | After resetting, the next access to the name property will generate a new name. |
| 71 | The old name is not returned to the pool to ensure uniqueness across all robots. |
| 72 | """ |
| 73 | # Edge Case: Robot currently has a name that needs to be wiped |
| 74 | self._name = None |
| 75 | |
| 76 | # Handled Edge Cases: Robot's name has not been assigned yet; Name pool has not been initialized; All possible names have been used; Robot currently has a name that needs to be wiped |