| 1 | import random |
| 2 | import string |
| 3 | |
| 1 | 4 | class Robot: |
| 5 | # Class variable to hold the pre-generated name pool and current index |
| 6 | _name_pool = [] |
| 7 | _current_index = 0 |
| 8 | |
| 2 | 9 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 10 | # Edge Case: Initialize the name pool if it's empty (first robot creation) |
| 11 | if not Robot._name_pool: |
| 12 | Robot._generate_name_pool() |
| 13 | |
| 14 | self._name = None |
| 15 | # Edge Case: Handle the case where all possible names are exhausted |
| 16 | if Robot._current_index >= len(Robot._name_pool): |
| 17 | raise RuntimeError("All possible robot names have been exhausted.") |
| 18 | |
| 19 | # Assign the next name from the pool |
| 20 | self._name = Robot._name_pool[Robot._current_index] |
| 21 | Robot._current_index += 1 |
| 22 | |
| 5 | 23 | @property |
| 6 | 24 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 25 | # Edge Case: Return the current name if it exists |
| 26 | if self._name is None: |
| 27 | raise RuntimeError("Robot name is not set.") |
| 28 | return self._name |
| 29 | |
| 9 | 30 | def reset(self) -> None: |
| 10 | | pass |
| 31 | # Edge Case: Handle reset when all possible names are exhausted |
| 32 | if Robot._current_index >= len(Robot._name_pool): |
| 33 | raise RuntimeError("All possible robot names have been exhausted.") |
| 34 | |
| 35 | # Wipe the current name and assign a new one |
| 36 | self._name = Robot._name_pool[Robot._current_index] |
| 37 | Robot._current_index += 1 |
| 38 | |
| 39 | @classmethod |
| 40 | def _generate_name_pool(cls): |
| 41 | """Generate all possible robot names and shuffle them randomly.""" |
| 42 | # Edge Case: Handle potential memory issues during name generation |
| 43 | try: |
| 44 | names = [] |
| 45 | for first_letter in string.ascii_uppercase: |
| 46 | for second_letter in string.ascii_uppercase: |
| 47 | for number in range(1000): |
| 48 | names.append(f"{first_letter}{second_letter}{number:03d}") |
| 49 | |
| 50 | # Shuffle the names randomly |
| 51 | random.shuffle(names) |
| 52 | cls._name_pool = names |
| 53 | except MemoryError: |
| 54 | raise RuntimeError("Not enough memory to generate the robot name pool.") |
| 55 | |
| 56 | # Handled Edge Cases: Empty name pool initialization, exhausted names, None name access, memory issues during generation |