| 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 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 unique names available") |
| 27 | |
| 2 | 28 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 29 | """ |
| 30 | Initialize a new robot with no name. |
| 31 | A random unique name is assigned upon first access. |
| 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. |
| 39 | |
| 40 | Returns: |
| 41 | str: The robot's unique name in format AA000. |
| 42 | """ |
| 43 | # Edge Case: Robot already has a name, return it |
| 44 | if self._name is not None: |
| 45 | return self._name |
| 46 | |
| 47 | # Edge Case: All possible names have been used |
| 48 | if len(Robot._name_pool - Robot._used_names) == 0: |
| 49 | raise RuntimeError("No more unique names available") |
| 50 | |
| 51 | # Select a random name that hasn't been used |
| 52 | available_names = Robot._name_pool - Robot._used_names |
| 53 | self._name = random.choice(list(available_names)) |
| 54 | Robot._used_names.add(self._name) |
| 55 | |
| 56 | return self._name |
| 57 | |
| 9 | 58 | def reset(self) -> None: |
| 10 | | pass |
| 59 | """ |
| 60 | Reset the robot to factory settings, wiping its name. |
| 61 | The name is returned to the pool of available names. |
| 62 | """ |
| 63 | # Edge Case: Robot has no name to reset |
| 64 | if self._name is None: |
| 65 | return |
| 66 | |
| 67 | # Return the name to the pool |
| 68 | Robot._used_names.discard(self._name) |
| 69 | self._name = None |
| 70 | |
| 71 | # Handled Edge Cases: Robot already has a name, all possible names exhausted, robot has no name to reset |