| 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 selected from a pre-generated pool. |
| 11 | """ |
| 12 | |
| 13 | # Class variable to hold all possible names and track used names |
| 14 | _name_pool = None |
| 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 on first access. |
| 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 randomly from the available 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 generation when all names are used |
| 37 | if self._name is None: |
| 38 | # Generate the name pool if it hasn't been created yet |
| 39 | if Robot._name_pool is None: |
| 40 | Robot._generate_name_pool() |
| 41 | |
| 42 | # Edge Case: No names left in the pool |
| 43 | if not Robot._name_pool: |
| 44 | raise RuntimeError("No more names available") |
| 45 | |
| 46 | # Select a random name from the pool |
| 47 | self._name = Robot._name_pool.pop() |
| 48 | Robot._used_names.add(self._name) |
| 49 | |
| 50 | return self._name |
| 51 | |
| 9 | 52 | def reset(self) -> None: |
| 10 | | pass |
| 53 | """ |
| 54 | Reset the robot to factory settings, wiping its name. |
| 55 | The name is returned to the pool for potential reuse. |
| 56 | """ |
| 57 | # Edge Case: Resetting a robot that never had a name assigned |
| 58 | if self._name is not None: |
| 59 | # Return the name to the pool |
| 60 | Robot._used_names.discard(self._name) |
| 61 | if Robot._name_pool is not None: # Only add back if pool exists |
| 62 | Robot._name_pool.add(self._name) |
| 63 | self._name = None |
| 64 | |
| 65 | @classmethod |
| 66 | def _generate_name_pool(cls): |
| 67 | """ |
| 68 | Generate all possible valid robot names and store them in a set. |
| 69 | Format: two uppercase letters followed by three digits. |
| 70 | """ |
| 71 | # Edge Case: Prevent regenerating the pool if it already exists |
| 72 | if cls._name_pool is not None: |
| 73 | return |
| 74 | |
| 75 | cls._name_pool = set() |
| 76 | for first_letter in string.ascii_uppercase: |
| 77 | for second_letter in string.ascii_uppercase: |
| 78 | for number in range(1000): |
| 79 | name = f"{first_letter}{second_letter}{number:03d}" |
| 80 | cls._name_pool.add(name) |
| 81 | |
| 82 | # Handled Edge Cases: Name generation when all names are used, No names left in the pool, |
| 83 | # Resetting a robot that never had a name assigned, |
| 84 | # Prevent regenerating the pool if it already exists |