| 1 | import random | |
| 2 | import string | |
| 3 | ||
| 1 | 4 | class Robot: |
| 5 | # Class variable to hold the set of used names | |
| 6 | _used_names = set() | |
| 7 | ||
| 8 | # Class variable to hold the pool of available names | |
| 9 | _name_pool = set() | |
| 10 | ||
| 11 | # Generate all possible names once | |
| 12 | @classmethod | |
| 13 | def _generate_name_pool(cls): | |
| 14 | if not cls._name_pool: # Only generate if not already done | |
| 15 | letters = string.ascii_uppercase | |
| 16 | digits = string.digits | |
| 17 | for l1 in letters: | |
| 18 | for l2 in letters: | |
| 19 | for d1 in digits: | |
| 20 | for d2 in digits: | |
| 21 | for d3 in digits: | |
| 22 | name = f"{l1}{l2}{d1}{d2}{d3}" | |
| 23 | cls._name_pool.add(name) | |
| 24 | ||
| 2 | 25 | def __init__(self): |
| 3 | pass | |
| 4 | ||
| 26 | # Edge Case: First initialization - generate the name pool | |
| 27 | Robot._generate_name_pool() | |
| 28 | self._name = None | |
| 29 | self.reset() | |
| 30 | ||
| 5 | 31 | @property |
| 6 | 32 | def name(self) -> str: |
| 7 | pass | |
| 8 | ||
| 33 | # Edge Case: Return the current name if already assigned | |
| 34 | return self._name | |
| 35 | ||
| 9 | 36 | def reset(self) -> None: |
| 10 | pass | |
| 37 | # Edge Case: Return current name to pool if it exists | |
| 38 | if self._name: | |
| 39 | Robot._used_names.discard(self._name) | |
| 40 | ||
| 41 | # Edge Case: No names available | |
| 42 | if len(Robot._used_names) >= len(Robot._name_pool): | |
| 43 | raise RuntimeError("No more names available") | |
| 44 | ||
| 45 | # Select a name that hasn't been used | |
| 46 | available_names = Robot._name_pool - Robot._used_names | |
| 47 | new_name = random.choice(list(available_names)) | |
| 48 | ||
| 49 | # Mark the name as used | |
| 50 | Robot._used_names.add(new_name) | |
| 51 | self._name = new_name | |
| 52 | ||
| 53 | # Handled Edge Cases: First initialization - generate the name pool, Return the current name if already assigned, Return current name to pool if it exists, No names available |