| 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). Each robot has a unique name. |
| 10 | """ |
| 11 | |
| 12 | # Class variable to store all possible names |
| 13 | _name_pool = [] |
| 14 | # Class variable to track used names |
| 15 | _used_names = set() |
| 16 | |
| 2 | 17 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 18 | """ |
| 19 | Initialize a new Robot instance with no name. |
| 20 | The actual name is generated when first accessed. |
| 21 | """ |
| 22 | self._name = None |
| 23 | |
| 5 | 24 | @property |
| 6 | 25 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 26 | """ |
| 27 | Get the robot's name. If the robot doesn't have a name yet, |
| 28 | generate one from the name pool. |
| 29 | |
| 30 | Returns: |
| 31 | str: The robot's name in format AA000. |
| 32 | |
| 33 | Raises: |
| 34 | RuntimeError: If all possible names have been used. |
| 35 | """ |
| 36 | # Edge Case: Name not yet assigned |
| 37 | if self._name is None: |
| 38 | # Edge Case: All names have been used |
| 39 | if len(Robot._used_names) >= 676000: # 26*26*10*10*10 |
| 40 | raise RuntimeError("No more names available") |
| 41 | |
| 42 | # Generate name pool if it's empty |
| 43 | # Edge Case: First time accessing name pool |
| 44 | if not Robot._name_pool: |
| 45 | letters = string.ascii_uppercase |
| 46 | digits = string.digits |
| 47 | # Generate all possible combinations |
| 48 | for l1 in letters: |
| 49 | for l2 in letters: |
| 50 | for d1 in digits: |
| 51 | for d2 in digits: |
| 52 | for d3 in digits: |
| 53 | Robot._name_pool.append(f"{l1}{l2}{d1}{d2}{d3}") |
| 54 | # Shuffle the pool to ensure randomness |
| 55 | random.shuffle(Robot._name_pool) |
| 56 | |
| 57 | # Assign the next available name |
| 58 | while True: |
| 59 | # Edge Case: Name pool exhausted but not all names used (should not happen with our approach) |
| 60 | if not Robot._name_pool: |
| 61 | raise RuntimeError("Name pool unexpectedly empty") |
| 62 | |
| 63 | candidate_name = Robot._name_pool.pop() |
| 64 | # Edge Case: Name already used (should be rare with our approach but possible with resets) |
| 65 | if candidate_name not in Robot._used_names: |
| 66 | self._name = candidate_name |
| 67 | Robot._used_names.add(self._name) |
| 68 | break |
| 69 | |
| 70 | return self._name |
| 71 | |
| 9 | 72 | def reset(self) -> None: |
| 10 | | pass |
| 73 | """ |
| 74 | Reset the robot to factory settings, wiping its name. |
| 75 | The next time the name is accessed, a new one will be generated. |
| 76 | """ |
| 77 | # Edge Case: Resetting a robot that never had a name assigned |
| 78 | if self._name is not None: |
| 79 | # Remove the name from used names so it can be reassigned |
| 80 | Robot._used_names.discard(self._name) |
| 81 | self._name = None |
| 82 | |
| 83 | # Handled Edge Cases: Name not yet assigned, All names have been used, First time accessing name pool, |
| 84 | # Name pool exhausted but not all names used, Name already used, Resetting a robot that never had a name assigned |