| 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 names can be reset to return to the pool of available names. |
| 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 range(10): |
| 20 | for digit2 in range(10): |
| 21 | for digit3 in range(10): |
| 22 | _name_pool.add(f"{letter1}{letter2}{digit1}{digit2}{digit3}") |
| 23 | |
| 24 | # Edge Case: Handle the scenario where all possible names are exhausted |
| 25 | if len(_name_pool) == 0: |
| 26 | raise RuntimeError("No more names available for robots") |
| 27 | |
| 2 | 28 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 29 | """ |
| 30 | Initialize a new robot with no name. |
| 31 | A name will be assigned when the name property is first accessed. |
| 32 | """ |
| 33 | self._name = None |
| 34 | |
| 5 | 35 | @property |
| 6 | 36 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 37 | """ |
| 38 | Get the robot's name. If the robot doesn't have a name yet, assign one randomly. |
| 39 | |
| 40 | Returns: |
| 41 | str: The robot's name in the format of two uppercase letters followed by three digits. |
| 42 | |
| 43 | Raises: |
| 44 | RuntimeError: If all possible names have been used. |
| 45 | """ |
| 46 | # Edge Case: Robot already has a name, return it |
| 47 | if self._name is not None: |
| 48 | return self._name |
| 49 | |
| 50 | # Edge Case: All names have been used |
| 51 | if len(Robot._name_pool - Robot._used_names) == 0: |
| 52 | raise RuntimeError("No more names available for robots") |
| 53 | |
| 54 | # Select a random name that hasn't been used |
| 55 | available_names = Robot._name_pool - Robot._used_names |
| 56 | self._name = random.choice(list(available_names)) |
| 57 | Robot._used_names.add(self._name) |
| 58 | |
| 59 | return self._name |
| 60 | |
| 9 | 61 | def reset(self) -> None: |
| 10 | | pass |
| 62 | """ |
| 63 | Reset the robot to its factory settings, wiping its name. |
| 64 | The name is returned to the pool of available names. |
| 65 | """ |
| 66 | # Edge Case: Robot has no name to reset |
| 67 | if self._name is None: |
| 68 | return |
| 69 | |
| 70 | # Remove the name from used names and reset the robot's name |
| 71 | Robot._used_names.discard(self._name) |
| 72 | self._name = None |
| 73 | |
| 74 | # Handled Edge Cases: Robot already has a name, all names have been used, robot has no name to reset |