| 1 | import random |
| 2 | import string |
| 3 | |
| 1 | 4 | class Robot: |
| 5 | # Class variable to hold the pre-generated name pool and track used names |
| 6 | _name_pool = [] |
| 7 | _used_names = set() |
| 8 | |
| 2 | 9 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 10 | # Edge Case: Initialize the name pool if it's empty |
| 11 | if not Robot._name_pool: |
| 12 | self._generate_name_pool() |
| 13 | self._name = None |
| 14 | # Edge Case: Handle the case where all names might be used (though practically impossible) |
| 15 | if len(Robot._used_names) >= len(Robot._name_pool): |
| 16 | raise RuntimeError("All possible robot names have been used.") |
| 17 | self._assign_name() |
| 18 | |
| 5 | 19 | @property |
| 6 | 20 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 21 | # Edge Case: Return the assigned name |
| 22 | return self._name |
| 23 | |
| 9 | 24 | def reset(self) -> None: |
| 10 | | pass |
| 25 | # Edge Case: Handle reset when name is None (not yet assigned) |
| 26 | if self._name is not None: |
| 27 | Robot._used_names.discard(self._name) |
| 28 | # Edge Case: Handle the case where all names might be used |
| 29 | if len(Robot._used_names) >= len(Robot._name_pool): |
| 30 | raise RuntimeError("All possible robot names have been used.") |
| 31 | self._assign_name() |
| 32 | |
| 33 | def _assign_name(self) -> None: |
| 34 | # Edge Case: Find an available name from the pool |
| 35 | while True: |
| 36 | candidate = Robot._name_pool.pop() |
| 37 | if candidate not in Robot._used_names: |
| 38 | self._name = candidate |
| 39 | Robot._used_names.add(self._name) |
| 40 | break |
| 41 | |
| 42 | @classmethod |
| 43 | def _generate_name_pool(cls) -> None: |
| 44 | # Generate all possible names in format: two uppercase letters + three digits |
| 45 | for first_letter in string.ascii_uppercase: |
| 46 | for second_letter in string.ascii_uppercase: |
| 47 | for digit1 in string.digits: |
| 48 | for digit2 in string.digits: |
| 49 | for digit3 in string.digits: |
| 50 | cls._name_pool.append(f"{first_letter}{second_letter}{digit1}{digit2}{digit3}") |
| 51 | # Shuffle the pool to ensure randomness |
| 52 | random.shuffle(cls._name_pool) |
| 53 | |
| 54 | # Handled Edge Cases: Empty name pool initialization, all names used, None name during reset, name assignment from pool |