| 1 | import random |
| 2 | import string |
| 3 | |
| 1 | 4 | class Robot: |
| 5 | # Class variable to store all used names |
| 6 | _used_names = set() |
| 7 | |
| 8 | # Class variable to store pre-generated names pool |
| 9 | _name_pool = [] |
| 10 | |
| 11 | # Edge Case: Initialize name pool if empty |
| 12 | # This handles the first instantiation of any robot |
| 13 | @classmethod |
| 14 | def _initialize_name_pool(cls): |
| 15 | if not cls._name_pool: |
| 16 | # Generate all possible names in random order |
| 17 | letters = string.ascii_uppercase |
| 18 | digits = string.digits |
| 19 | |
| 20 | # Create all possible combinations |
| 21 | all_names = [l1 + l2 + d1 + d2 + d3 |
| 22 | for l1 in letters |
| 23 | for l2 in letters |
| 24 | for d1 in digits |
| 25 | for d2 in digits |
| 26 | for d3 in digits] |
| 27 | |
| 28 | # Shuffle to ensure randomness |
| 29 | random.shuffle(all_names) |
| 30 | cls._name_pool = all_names |
| 31 | |
| 2 | 32 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 33 | # Edge Case: Handle initialization when no names are available |
| 34 | # This would only happen if all possible names are used (26*26*10*10*10 = 676,000 names) |
| 35 | self._initialize_name_pool() |
| 36 | self._name = None |
| 37 | self.reset() |
| 38 | |
| 5 | 39 | @property |
| 6 | 40 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 41 | # Edge Case: Return current name if already assigned |
| 42 | # This ensures the name stays consistent between accesses |
| 43 | return self._name |
| 44 | |
| 9 | 45 | def reset(self) -> None: |
| 10 | | pass |
| 46 | # Edge Case: Handle name release when robot had a previous name |
| 47 | # This ensures the old name is returned to the pool of available names |
| 48 | if self._name: |
| 49 | Robot._used_names.discard(self._name) |
| 50 | |
| 51 | # Edge Case: Handle exhaustion of name pool |
| 52 | # This would occur if all 676,000 possible names are used |
| 53 | if not Robot._name_pool: |
| 54 | raise RuntimeError("No more names available") |
| 55 | |
| 56 | # Get a new name from the pool |
| 57 | new_name = Robot._name_pool.pop() |
| 58 | |
| 59 | # Edge Case: Handle collision with already used name |
| 60 | # This is a safety check, though with the pre-generated pool it shouldn't happen |
| 61 | while new_name in Robot._used_names: |
| 62 | if not Robot._name_pool: |
| 63 | raise RuntimeError("No more names available") |
| 64 | new_name = Robot._name_pool.pop() |
| 65 | |
| 66 | # Assign the new name |
| 67 | self._name = new_name |
| 68 | Robot._used_names.add(self._name) |
| 69 | |
| 70 | # Handled Edge Cases: Initialization without a name, consistent name access, |
| 71 | # name release during reset, exhaustion of name pool, collision handling |