| 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). Names are unique and |
| 10 | randomly selected from a pre-generated pool. When reset, the name is |
| 11 | returned to the pool and a new name is assigned. |
| 12 | """ |
| 13 | |
| 14 | # Class variable to hold all possible names and track used names |
| 15 | _name_pool = set() |
| 16 | _used_names = set() |
| 17 | |
| 18 | # Generate all possible names once at class definition time |
| 19 | if not _name_pool: |
| 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.add(f"{letter1}{letter2}{digit1}{digit2}{digit3}") |
| 26 | |
| 2 | 27 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 28 | """ |
| 29 | Initialize a new Robot instance with no name. |
| 30 | """ |
| 31 | self._name = None |
| 32 | # Assign a name when the robot is created |
| 33 | self._assign_name() |
| 34 | |
| 5 | 35 | @property |
| 6 | 36 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 37 | """ |
| 38 | Get the robot's current name. |
| 39 | |
| 40 | Returns: |
| 41 | str: The robot's name in format of two uppercase letters followed by three digits. |
| 42 | """ |
| 43 | return self._name |
| 44 | |
| 9 | 45 | def reset(self) -> None: |
| 10 | | pass |
| 46 | """ |
| 47 | Reset the robot to factory settings, wiping its name and assigning a new one. |
| 48 | |
| 49 | The current name is returned to the pool of available names. |
| 50 | """ |
| 51 | # Edge Case: Resetting a robot that somehow has no name |
| 52 | if self._name is not None: |
| 53 | Robot._used_names.discard(self._name) |
| 54 | self._assign_name() |
| 55 | |
| 56 | def _assign_name(self) -> None: |
| 57 | """ |
| 58 | Assign a new random name to the robot from the available pool. |
| 59 | |
| 60 | Raises: |
| 61 | RuntimeError: If all possible names have been used. |
| 62 | """ |
| 63 | # Edge Case: All possible names have been used |
| 64 | if len(Robot._used_names) >= len(Robot._name_pool): |
| 65 | raise RuntimeError("No more unique names available") |
| 66 | |
| 67 | # Select a name that hasn't been used yet |
| 68 | available_names = Robot._name_pool - Robot._used_names |
| 69 | new_name = random.choice(list(available_names)) |
| 70 | |
| 71 | # Mark the name as used |
| 72 | Robot._used_names.add(new_name) |
| 73 | self._name = new_name |
| 74 | |
| 75 | # Handled Edge Cases: Resetting a robot with no name, all possible names exhausted |