| 1 | import random |
| 2 | import string |
| 3 | |
| 1 | 4 | class Robot: |
| 5 | """ |
| 6 | A class representing a robot with a randomly generated name. |
| 7 | |
| 8 | The robot's name consists of two uppercase letters followed by three digits. |
| 9 | Names are unique and randomly selected from a pre-generated pool. |
| 10 | """ |
| 11 | |
| 12 | # Class-level set to track all used names across all robot instances |
| 13 | _used_names = set() |
| 14 | |
| 15 | # Class-level list of all possible names (generated once) |
| 16 | _name_pool = [] |
| 17 | |
| 2 | 18 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 19 | """ |
| 20 | Initialize a new Robot instance with no name. |
| 21 | The actual name is generated on first access. |
| 22 | """ |
| 23 | self._name = None |
| 24 | |
| 5 | 25 | @property |
| 6 | 26 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 27 | """ |
| 28 | Get the robot's name. If the robot doesn't have a name yet, |
| 29 | generate one randomly from the available pool. |
| 30 | |
| 31 | Returns: |
| 32 | str: The robot's name in format AA000 |
| 33 | |
| 34 | Raises: |
| 35 | RuntimeError: If all possible names are exhausted |
| 36 | """ |
| 37 | # Edge Case: Name not yet assigned - generate a new one |
| 38 | if self._name is None: |
| 39 | # Edge Case: All names exhausted |
| 40 | if len(Robot._name_pool) == 0 and len(Robot._used_names) >= 676000: |
| 41 | raise RuntimeError("All possible robot names have been exhausted") |
| 42 | |
| 43 | # Generate the name pool if it hasn't been created yet |
| 44 | # Edge Case: First robot initialization - create name pool |
| 45 | if not Robot._name_pool: |
| 46 | letters = string.ascii_uppercase |
| 47 | digits = string.digits |
| 48 | Robot._name_pool = [l1 + l2 + d1 + d2 + d3 |
| 49 | for l1 in letters |
| 50 | for l2 in letters |
| 51 | for d1 in digits |
| 52 | for d2 in digits |
| 53 | for d3 in digits] |
| 54 | random.shuffle(Robot._name_pool) |
| 55 | |
| 56 | # Find an available name |
| 57 | while Robot._name_pool: |
| 58 | candidate = Robot._name_pool.pop() |
| 59 | if candidate not in Robot._used_names: |
| 60 | self._name = candidate |
| 61 | Robot._used_names.add(candidate) |
| 62 | break |
| 63 | |
| 64 | return self._name |
| 65 | |
| 9 | 66 | def reset(self) -> None: |
| 10 | | pass |
| 67 | """ |
| 68 | Reset the robot to factory settings, wiping its name. |
| 69 | The name is returned to the pool of available names. |
| 70 | """ |
| 71 | # Edge Case: Resetting a robot that never had a name assigned |
| 72 | if self._name is not None: |
| 73 | # Return the name to the pool of available names |
| 74 | Robot._used_names.discard(self._name) |
| 75 | # Note: We don't add it back to _name_pool to maintain randomness |
| 76 | # and avoid regenerating the pool |
| 77 | self._name = None |
| 78 | |
| 79 | # Handled Edge Cases: Name not yet assigned, all names exhausted, first robot initialization, |
| 80 | # resetting robot that never had a name assigned |