| 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 | Names are unique and randomly selected from a pre-generated pool. |
| 10 | """ |
| 11 | |
| 12 | # Class-level set to keep track of all used names across all robot instances |
| 13 | _used_names = set() |
| 14 | |
| 15 | # Class-level list of all possible valid names |
| 16 | _name_pool = [] |
| 17 | |
| 2 | 18 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 19 | """ |
| 20 | Initialize a new Robot instance with no name. |
| 21 | The actual name is generated on first access. |
| 22 | """ |
| 23 | self._name = None |
| 24 | |
| 5 | 25 | @property |
| 6 | 26 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 27 | """ |
| 28 | Get the robot's name. If the robot doesn't have a name yet, |
| 29 | generate one randomly from the available pool. |
| 30 | |
| 31 | Returns: |
| 32 | str: The robot's name in format AA000 |
| 33 | |
| 34 | Raises: |
| 35 | RuntimeError: If all possible names are already used |
| 36 | """ |
| 37 | # Edge Case: All possible names have been used |
| 38 | if len(self._used_names) >= 676000: # 26*26*10*10*10 |
| 39 | raise RuntimeError("No more unique names available") |
| 40 | |
| 41 | if self._name is None: |
| 42 | # Edge Case: Name pool not yet initialized |
| 43 | if not self._name_pool: |
| 44 | self._generate_name_pool() |
| 45 | |
| 46 | # Generate a new name by randomly selecting from available names |
| 47 | available_names = list(set(self._name_pool) - self._used_names) |
| 48 | self._name = random.choice(available_names) |
| 49 | self._used_names.add(self._name) |
| 50 | |
| 51 | return self._name |
| 52 | |
| 9 | 53 | def reset(self) -> None: |
| 10 | | pass |
| 54 | """ |
| 55 | Reset the robot to factory settings, wiping its name. |
| 56 | The name is released back to the pool and can be used by other robots. |
| 57 | """ |
| 58 | # Edge Case: Robot never had a name assigned |
| 59 | if self._name is not None: |
| 60 | self._used_names.discard(self._name) |
| 61 | self._name = None |
| 62 | |
| 63 | @classmethod |
| 64 | def _generate_name_pool(cls): |
| 65 | """ |
| 66 | Generate all possible valid robot names and store them in the class-level pool. |
| 67 | Format: two uppercase letters followed by three digits. |
| 68 | """ |
| 69 | # Edge Case: Name pool already generated |
| 70 | if cls._name_pool: |
| 71 | return |
| 72 | |
| 73 | for first_letter in string.ascii_uppercase: |
| 74 | for second_letter in string.ascii_uppercase: |
| 75 | for digit1 in string.digits: |
| 76 | for digit2 in string.digits: |
| 77 | for digit3 in string.digits: |
| 78 | name = f"{first_letter}{second_letter}{digit1}{digit2}{digit3}" |
| 79 | cls._name_pool.append(name) |
| 80 | # Handled Edge Cases: Name pool already generated |
| 81 | |
| 82 | # Handled Edge Cases: All possible names have been used, Name pool not yet initialized, Robot never had a name assigned |