| 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 | The name can be reset to a new random unique name using the reset() method. |
| 12 | """ |
| 13 | |
| 14 | # Class variable to hold all possible names |
| 15 | _name_pool = [] |
| 16 | # Class variable to track used names |
| 17 | _used_names = set() |
| 18 | |
| 2 | 19 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 20 | """ |
| 21 | Initialize a new Robot instance with no name. |
| 22 | The actual name is generated when first accessed via the name property. |
| 23 | """ |
| 24 | self._name = None |
| 25 | |
| 5 | 26 | @property |
| 6 | 27 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 28 | """ |
| 29 | Get the robot's name. If the robot doesn't have a name yet, |
| 30 | generate one from the name pool. |
| 31 | |
| 32 | Returns: |
| 33 | str: The robot's name in format AA000 |
| 34 | |
| 35 | Raises: |
| 36 | RuntimeError: If all possible names have been used |
| 37 | """ |
| 38 | # Edge Case: Robot already has a name |
| 39 | if self._name is not None: |
| 40 | return self._name |
| 41 | |
| 42 | # Edge Case: First time accessing name - need to generate all possible names |
| 43 | if not Robot._name_pool: |
| 44 | # Generate all possible names in format AA000 |
| 45 | for first_letter in string.ascii_uppercase: |
| 46 | for second_letter in string.ascii_uppercase: |
| 47 | for number in range(1000): |
| 48 | name = f"{first_letter}{second_letter}{number:03d}" |
| 49 | Robot._name_pool.append(name) |
| 50 | # Shuffle the names randomly |
| 51 | random.shuffle(Robot._name_pool) |
| 52 | |
| 53 | # Edge Case: All possible names have been used |
| 54 | if len(Robot._used_names) >= len(Robot._name_pool): |
| 55 | raise RuntimeError("All possible robot names have been used") |
| 56 | |
| 57 | # Get the next available name from the pool |
| 58 | while Robot._name_pool: |
| 59 | candidate_name = Robot._name_pool.pop() |
| 60 | if candidate_name not in Robot._used_names: |
| 61 | self._name = candidate_name |
| 62 | Robot._used_names.add(self._name) |
| 63 | break |
| 64 | |
| 65 | return self._name |
| 66 | |
| 9 | 67 | def reset(self) -> None: |
| 10 | | pass |
| 68 | """ |
| 69 | Reset the robot to factory settings, wiping its name. |
| 70 | The next time the name is accessed, a new random name will be generated. |
| 71 | """ |
| 72 | # Edge Case: Robot has no name to reset |
| 73 | if self._name is not None: |
| 74 | # Remove the current name from used names |
| 75 | Robot._used_names.discard(self._name) |
| 76 | |
| 77 | # Clear the robot's name |
| 78 | self._name = None |
| 79 | |
| 80 | # Handled Edge Cases: Robot already has a name, first time accessing name, all possible names have been used, robot has no name to reset |