| 1 | import random |
| 2 | import string |
| 3 | |
| 1 | 4 | class Robot: |
| 5 | """ |
| 6 | A class representing a robot with a randomly generated name. |
| 7 | |
| 8 | The robot's name consists of two uppercase letters followed by three digits. |
| 9 | The name is generated when first accessed and can be reset to a new random name. |
| 10 | All names are unique across all robot instances. |
| 11 | """ |
| 12 | |
| 13 | # Class-level set to track all used names |
| 14 | _used_names = set() |
| 15 | |
| 16 | # Pre-generate all possible valid names |
| 17 | _all_names = [ |
| 18 | f"{letter1}{letter2}{digit1}{digit2}{digit3}" |
| 19 | for letter1 in string.ascii_uppercase |
| 20 | for letter2 in string.ascii_uppercase |
| 21 | for digit1 in string.digits |
| 22 | for digit2 in string.digits |
| 23 | for digit3 in string.digits |
| 24 | ] |
| 25 | |
| 26 | # Edge Case: Handle the case where all possible names are exhausted |
| 27 | if len(_all_names) <= 1000000: # 26*26*10*10*10 = 676,000 possible names |
| 28 | _name_pool = set(_all_names) |
| 29 | else: |
| 30 | raise RuntimeError("Failed to generate name pool") |
| 31 | |
| 2 | 32 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 33 | """ |
| 34 | Initialize a new Robot instance with no name. |
| 35 | The name will be generated when first accessed. |
| 36 | """ |
| 37 | self._name = None |
| 38 | |
| 5 | 39 | @property |
| 6 | 40 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 41 | """ |
| 42 | Get the robot's name. If the robot doesn't have a name yet, |
| 43 | generate a new unique random name. |
| 44 | |
| 45 | Returns: |
| 46 | str: The robot's name in format AA000 |
| 47 | |
| 48 | Raises: |
| 49 | RuntimeError: If all possible names are already in use |
| 50 | """ |
| 51 | # Edge Case: Robot already has a name |
| 52 | if self._name is not None: |
| 53 | return self._name |
| 54 | |
| 55 | # Edge Case: All possible names are already used |
| 56 | if len(Robot._name_pool) == 0: |
| 57 | raise RuntimeError("All possible robot names are already in use") |
| 58 | |
| 59 | # Select a random name from the pool |
| 60 | available_names = list(Robot._name_pool) |
| 61 | self._name = random.choice(available_names) |
| 62 | Robot._used_names.add(self._name) |
| 63 | Robot._name_pool.remove(self._name) |
| 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 unique random name will be generated. |
| 71 | """ |
| 72 | # Edge Case: Robot has no name to reset |
| 73 | if self._name is None: |
| 74 | return |
| 75 | |
| 76 | # Return the name to the pool |
| 77 | Robot._name_pool.add(self._name) |
| 78 | Robot._used_names.discard(self._name) |
| 79 | self._name = None |
| 80 | |
| 81 | # Handled Edge Cases: Robot already has a name, all possible names are already used, robot has no name to reset |