| 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 | Each robot has a unique name, and the name can be reset to generate a new one. |
| 10 | """ |
| 11 | |
| 12 | # Class-level set to keep track of all used names across all robot instances |
| 13 | _used_names = set() |
| 14 | |
| 15 | # Pre-generate all possible valid names |
| 16 | _name_pool = [ |
| 17 | f"{letter1}{letter2}{digit1}{digit2}{digit3}" |
| 18 | for letter1 in string.ascii_uppercase |
| 19 | for letter2 in string.ascii_uppercase |
| 20 | for digit1 in string.digits |
| 21 | for digit2 in string.digits |
| 22 | for digit3 in string.digits |
| 23 | ] |
| 24 | |
| 25 | # Shuffle the name pool to ensure randomness |
| 26 | random.shuffle(_name_pool) |
| 27 | |
| 28 | # Index to keep track of the next available name in the pool |
| 29 | _next_name_index = 0 |
| 30 | |
| 2 | 31 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 32 | """ |
| 33 | Initialize a new Robot instance with no name. |
| 34 | The name will be generated on first access. |
| 35 | """ |
| 36 | self._name = None |
| 37 | |
| 5 | 38 | @property |
| 6 | 39 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 40 | """ |
| 41 | Get the robot's name. If the robot doesn't have a name yet, |
| 42 | generate one from the name pool. |
| 43 | |
| 44 | Returns: |
| 45 | str: The robot's name in the format of two uppercase letters followed by three digits. |
| 46 | |
| 47 | Raises: |
| 48 | RuntimeError: If all possible names have been used. |
| 49 | """ |
| 50 | # Edge Case: Robot has no name yet, need to assign one |
| 51 | if self._name is None: |
| 52 | # Edge Case: All possible names have been used |
| 53 | if Robot._next_name_index >= len(Robot._name_pool): |
| 54 | raise RuntimeError("No more unique names available") |
| 55 | |
| 56 | # Assign the next available name from the pool |
| 57 | self._name = Robot._name_pool[Robot._next_name_index] |
| 58 | Robot._used_names.add(self._name) |
| 59 | Robot._next_name_index += 1 |
| 60 | |
| 61 | return self._name |
| 62 | |
| 9 | 63 | def reset(self) -> None: |
| 10 | | pass |
| 64 | """ |
| 65 | Reset the robot to its factory settings, wiping its name. |
| 66 | The next time the name is accessed, a new one will be generated. |
| 67 | """ |
| 68 | # Edge Case: Robot has a name that needs to be released back to the pool |
| 69 | if self._name is not None: |
| 70 | Robot._used_names.discard(self._name) |
| 71 | self._name = None |
| 72 | |
| 73 | # Handled Edge Cases: Robot has no name yet, need to assign one; All possible names have been used; Robot has a name that needs to be released back to the pool |