| 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 | Names are unique and randomly selected from a pre-generated pool. |
| 10 | """ |
| 11 | |
| 12 | # Class-level set to track all used names across all robot instances |
| 13 | _used_names = set() |
| 14 | |
| 15 | # Pre-generate all possible valid names |
| 16 | _name_pool = set() |
| 17 | for letter1 in string.ascii_uppercase: |
| 18 | for letter2 in string.ascii_uppercase: |
| 19 | for digit1 in string.digits: |
| 20 | for digit2 in string.digits: |
| 21 | for digit3 in string.digits: |
| 22 | _name_pool.add(letter1 + letter2 + digit1 + digit2 + digit3) |
| 23 | |
| 2 | 24 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 25 | """ |
| 26 | Initialize a new Robot instance with no name. |
| 27 | A name will be generated when first accessed. |
| 28 | """ |
| 29 | self._name = None |
| 30 | |
| 5 | 31 | @property |
| 6 | 32 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 33 | """ |
| 34 | Get the robot's name. If the robot doesn't have a name yet, generate one. |
| 35 | |
| 36 | Returns: |
| 37 | str: The robot's name in format AA000. |
| 38 | |
| 39 | Raises: |
| 40 | RuntimeError: If all possible names are already in use. |
| 41 | """ |
| 42 | # Edge Case: Robot has no name yet, need to generate one |
| 43 | if self._name is None: |
| 44 | # Edge Case: All possible names are already used |
| 45 | if len(Robot._used_names) >= len(Robot._name_pool): |
| 46 | raise RuntimeError("All possible robot names are already in use.") |
| 47 | |
| 48 | # Select a random name that's not already used |
| 49 | available_names = Robot._name_pool - Robot._used_names |
| 50 | self._name = random.choice(list(available_names)) |
| 51 | Robot._used_names.add(self._name) |
| 52 | |
| 53 | return self._name |
| 54 | |
| 9 | 55 | def reset(self) -> None: |
| 10 | | pass |
| 56 | """ |
| 57 | Reset the robot to factory settings, wiping its name. |
| 58 | The name is returned to the pool and can be reused by other robots. |
| 59 | """ |
| 60 | # Edge Case: Robot has a name that needs to be released back to the pool |
| 61 | if self._name is not None: |
| 62 | Robot._used_names.discard(self._name) |
| 63 | self._name = None |
| 64 | |
| 65 | # Handled Edge Cases: Robot has no name yet, need to generate one; All possible names are already used; Robot has a name that needs to be released back to the pool |