| 1 | import random |
| 2 | import string |
| 3 | |
| 1 | 4 | class Robot: |
| 5 | """ |
| 6 | A class representing a robot with a randomly assigned name. |
| 7 | |
| 8 | The robot's name is generated in the format of two uppercase letters |
| 9 | followed by three digits (e.g., RX837 or BC811). Names are unique and |
| 10 | randomly assigned from a pre-generated pool. |
| 11 | """ |
| 12 | |
| 13 | # Class variable to hold the pool of all possible names |
| 14 | _name_pool = [] |
| 15 | # Class variable to track which names are already assigned |
| 16 | _assigned_names = set() |
| 17 | |
| 2 | 18 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 19 | """ |
| 20 | Initialize a new Robot instance with no name. |
| 21 | """ |
| 22 | # Edge Case: Initialize name pool if it's empty |
| 23 | if not Robot._name_pool: |
| 24 | self._generate_name_pool() |
| 25 | |
| 26 | self._name = None |
| 27 | # Edge Case: Handle case when all names are exhausted |
| 28 | if len(Robot._assigned_names) >= len(Robot._name_pool): |
| 29 | raise RuntimeError("All possible robot names have been assigned.") |
| 30 | |
| 31 | # Assign a name upon initialization |
| 32 | self._assign_name() |
| 33 | |
| 5 | 34 | @property |
| 6 | 35 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 36 | """ |
| 37 | Get the robot's current name. |
| 38 | |
| 39 | Returns: |
| 40 | str: The robot's name in format of two uppercase letters followed by three digits. |
| 41 | """ |
| 42 | return self._name |
| 43 | |
| 9 | 44 | def reset(self) -> None: |
| 10 | | pass |
| 45 | """ |
| 46 | Reset the robot to its factory settings, wiping its name. |
| 47 | |
| 48 | The robot will be assigned a new random name from the pool. |
| 49 | """ |
| 50 | # Edge Case: Handle case when all names are exhausted |
| 51 | if len(Robot._assigned_names) >= len(Robot._name_pool): |
| 52 | raise RuntimeError("All possible robot names have been assigned.") |
| 53 | |
| 54 | # Release the current name back to the pool |
| 55 | if self._name: |
| 56 | Robot._assigned_names.discard(self._name) |
| 57 | |
| 58 | # Assign a new name |
| 59 | self._assign_name() |
| 60 | |
| 61 | def _assign_name(self) -> None: |
| 62 | """ |
| 63 | Assign a new unique name to the robot from the name pool. |
| 64 | """ |
| 65 | # Find an unassigned name |
| 66 | available_names = list(set(Robot._name_pool) - Robot._assigned_names) |
| 67 | # Edge Case: Handle empty available names (should not happen due to earlier checks) |
| 68 | if not available_names: |
| 69 | raise RuntimeError("No available names to assign.") |
| 70 | |
| 71 | new_name = random.choice(available_names) |
| 72 | self._name = new_name |
| 73 | Robot._assigned_names.add(new_name) |
| 74 | |
| 75 | @classmethod |
| 76 | def _generate_name_pool(cls) -> None: |
| 77 | """ |
| 78 | Generate all possible valid robot names and shuffle them randomly. |
| 79 | |
| 80 | The format is two uppercase letters followed by three digits. |
| 81 | """ |
| 82 | # Edge Case: Prevent regenerating if already generated |
| 83 | if cls._name_pool: |
| 84 | return |
| 85 | |
| 86 | names = [] |
| 87 | # Generate all combinations of two letters (AA to ZZ) |
| 88 | for first_letter in string.ascii_uppercase: |
| 89 | for second_letter in string.ascii_uppercase: |
| 90 | # Generate all combinations of three digits (000 to 999) |
| 91 | for number in range(1000): |
| 92 | name = f"{first_letter}{second_letter}{number:03d}" |
| 93 | names.append(name) |
| 94 | |
| 95 | # Shuffle the names randomly |
| 96 | random.shuffle(names) |
| 97 | cls._name_pool = names |
| 98 | |
| 99 | # Handled Edge Cases: Empty name pool initialization, all names exhausted, empty available names, prevent regenerating pool |