| 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. When reset, the name is |
| 11 | returned to the pool and a new name is assigned. |
| 12 | """ |
| 13 | |
| 14 | # Class variable to hold all possible names and track used names |
| 15 | _name_pool = set() |
| 16 | _used_names = set() |
| 17 | |
| 18 | # Generate all possible names once at class definition time |
| 19 | if not _name_pool: |
| 20 | for letter1 in string.ascii_uppercase: |
| 21 | for letter2 in string.ascii_uppercase: |
| 22 | for digit1 in string.digits: |
| 23 | for digit2 in string.digits: |
| 24 | for digit3 in string.digits: |
| 25 | _name_pool.add(letter1 + letter2 + digit1 + digit2 + digit3) |
| 26 | |
| 2 | 27 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 28 | """ |
| 29 | Initialize a new Robot instance with no name. |
| 30 | A name will be assigned when first accessed. |
| 31 | """ |
| 32 | self._name = None |
| 33 | |
| 5 | 34 | @property |
| 6 | 35 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 36 | """ |
| 37 | Get the robot's name. If the robot doesn't have a name yet, |
| 38 | assign a random unique name from the pool. |
| 39 | |
| 40 | Returns: |
| 41 | str: The robot's name in format AA000. |
| 42 | |
| 43 | Raises: |
| 44 | RuntimeError: If all possible names are already in use. |
| 45 | """ |
| 46 | # Edge Case: Robot has no name yet, assign a new one |
| 47 | if self._name is None: |
| 48 | # Edge Case: All names are used |
| 49 | if len(Robot._used_names) >= len(Robot._name_pool): |
| 50 | raise RuntimeError("No more names available") |
| 51 | |
| 52 | # Select a name that hasn't been used yet |
| 53 | available_names = Robot._name_pool - Robot._used_names |
| 54 | self._name = random.choice(list(available_names)) |
| 55 | Robot._used_names.add(self._name) |
| 56 | |
| 57 | return self._name |
| 58 | |
| 9 | 59 | def reset(self) -> None: |
| 10 | | pass |
| 60 | """ |
| 61 | Reset the robot to factory settings, wiping its name. |
| 62 | The name is returned to the pool and can be reassigned to other robots. |
| 63 | """ |
| 64 | # Edge Case: Robot has a name that needs to be returned to the pool |
| 65 | if self._name is not None: |
| 66 | Robot._used_names.discard(self._name) |
| 67 | self._name = None |
| 68 | |
| 69 | # Handled Edge Cases: Robot has no name yet, assign a new one; All names are used |
| 70 | # Handled Edge Cases: Robot has a name that needs to be returned to the pool |