| 1 | import random |
| 2 | import string |
| 3 | |
| 1 | 4 | class Robot: |
| 5 | # Class variable to hold the name pool and current index |
| 6 | _name_pool = [] |
| 7 | _current_index = 0 |
| 8 | |
| 2 | 9 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 10 | """ |
| 11 | Initialize a Robot instance. |
| 12 | |
| 13 | Each robot starts with no name until accessed. |
| 14 | """ |
| 15 | self._assigned_name = None |
| 16 | |
| 17 | # Generate the name pool if it hasn't been generated yet |
| 18 | if not Robot._name_pool: |
| 19 | self._generate_name_pool() |
| 20 | |
| 5 | 21 | @property |
| 6 | 22 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 23 | """ |
| 24 | Get the robot's name. |
| 25 | |
| 26 | If the robot doesn't have a name yet, assign one from the pool. |
| 27 | |
| 28 | Returns: |
| 29 | str: The robot's name in format AA000. |
| 30 | """ |
| 31 | # Edge Case: Robot has no name assigned yet |
| 32 | if self._assigned_name is None: |
| 33 | # Edge Case: All possible names have been used |
| 34 | if Robot._current_index >= len(Robot._name_pool): |
| 35 | raise RuntimeError("No more unique names available") |
| 36 | |
| 37 | self._assigned_name = Robot._name_pool[Robot._current_index] |
| 38 | Robot._current_index += 1 |
| 39 | |
| 40 | return self._assigned_name |
| 41 | |
| 9 | 42 | def reset(self) -> None: |
| 10 | | pass |
| 43 | """ |
| 44 | Reset the robot to factory settings. |
| 45 | |
| 46 | This wipes the robot's name, allowing it to get a new one. |
| 47 | """ |
| 48 | # Edge Case: Resetting a robot that had a name |
| 49 | self._assigned_name = None |
| 50 | |
| 51 | @classmethod |
| 52 | def _generate_name_pool(cls): |
| 53 | """ |
| 54 | Generate all possible robot names and shuffle them randomly. |
| 55 | |
| 56 | This ensures names are unique and randomly distributed. |
| 57 | """ |
| 58 | # Edge Case: Name pool generation when it's empty |
| 59 | cls._name_pool = [] |
| 60 | |
| 61 | # Generate all combinations of two letters |
| 62 | letters = string.ascii_uppercase |
| 63 | # Generate all combinations of three digits |
| 64 | digits = [f"{i:03d}" for i in range(1000)] |
| 65 | |
| 66 | # Create all possible names |
| 67 | for letter1 in letters: |
| 68 | for letter2 in letters: |
| 69 | for digit in digits: |
| 70 | cls._name_pool.append(f"{letter1}{letter2}{digit}") |
| 71 | |
| 72 | # Shuffle the pool randomly |
| 73 | random.shuffle(cls._name_pool) |
| 74 | |
| 75 | # Handled Edge Cases: Robot has no name assigned yet, All possible names have been used, Resetting a robot that had a name, Name pool generation when it's empty |