| 1 | import random |
| 2 | import string |
| 3 | |
| 1 | 4 | class Robot: |
| 5 | # Class variable to hold the 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 possible names are used |
| 15 | if len(Robot._used_names) >= len(Robot._name_pool): |
| 16 | raise RuntimeError("No more unique names available") |
| 17 | self._assign_name() |
| 18 | |
| 5 | 19 | @property |
| 6 | 20 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 21 | return self._name |
| 22 | |
| 9 | 23 | def reset(self) -> None: |
| 10 | | pass |
| 24 | # Edge Case: Handle resetting a robot's name and making the old name available again |
| 25 | if self._name: |
| 26 | Robot._used_names.discard(self._name) |
| 27 | # Edge Case: Handle the case where all possible names are used |
| 28 | if len(Robot._used_names) >= len(Robot._name_pool): |
| 29 | raise RuntimeError("No more unique names available") |
| 30 | self._assign_name() |
| 31 | |
| 32 | def _assign_name(self) -> None: |
| 33 | # Edge Case: Handle assigning a name from the pool |
| 34 | while True: |
| 35 | if not Robot._name_pool: |
| 36 | # This should not happen if the pool is correctly initialized |
| 37 | raise RuntimeError("Name pool is unexpectedly empty") |
| 38 | |
| 39 | candidate_name = Robot._name_pool.pop() |
| 40 | if candidate_name not in Robot._used_names: |
| 41 | self._name = candidate_name |
| 42 | Robot._used_names.add(self._name) |
| 43 | break |
| 44 | |
| 45 | @classmethod |
| 46 | def _generate_name_pool(cls) -> None: |
| 47 | # Edge Case: Handle generating all possible names and shuffling them |
| 48 | cls._name_pool = [] |
| 49 | for first_letter in string.ascii_uppercase: |
| 50 | for second_letter in string.ascii_uppercase: |
| 51 | for number in range(1000): |
| 52 | name = f"{first_letter}{second_letter}{number:03d}" |
| 53 | cls._name_pool.append(name) |
| 54 | random.shuffle(cls._name_pool) |
| 55 | |
| 56 | # Handled Edge Cases: Empty name pool initialization, all names used, resetting names, assigning names from pool, generating and shuffling all possible names |