| 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 | The name can be reset to a new random unique name. |
| 12 | """ |
| 13 | |
| 14 | # Class variable to hold all possible names |
| 15 | _name_pool = [] |
| 16 | # Class variable to track used names |
| 17 | _used_names = set() |
| 18 | |
| 2 | 19 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 20 | """ |
| 21 | Initialize a new Robot instance with no name. |
| 22 | The actual name is generated when first accessed. |
| 23 | """ |
| 24 | self._name = None |
| 25 | |
| 5 | 26 | @property |
| 6 | 27 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 28 | """ |
| 29 | Get the robot's name. If the robot doesn't have a name yet, |
| 30 | generate one from the name pool. |
| 31 | |
| 32 | Returns: |
| 33 | str: The robot's name in format of two uppercase letters followed by three digits. |
| 34 | |
| 35 | Raises: |
| 36 | RuntimeError: If all possible names have been used. |
| 37 | """ |
| 38 | # Edge Case: Robot has no name yet, need to assign one |
| 39 | if self._name is None: |
| 40 | # Edge Case: All possible names have been used |
| 41 | if len(Robot._used_names) >= 676000: # 26*26*10*10*10 |
| 42 | raise RuntimeError("No more unique names available") |
| 43 | |
| 44 | # Edge Case: Name pool not yet initialized |
| 45 | if not Robot._name_pool: |
| 46 | self._generate_name_pool() |
| 47 | |
| 48 | # Find an unused name |
| 49 | while True: |
| 50 | candidate = Robot._name_pool.pop() |
| 51 | if candidate not in Robot._used_names: |
| 52 | self._name = candidate |
| 53 | Robot._used_names.add(self._name) |
| 54 | break |
| 55 | |
| 56 | return self._name |
| 57 | |
| 9 | 58 | def reset(self) -> None: |
| 10 | | pass |
| 59 | """ |
| 60 | Reset the robot to factory settings, wiping its name. |
| 61 | The next time the name is accessed, a new random unique name will be generated. |
| 62 | """ |
| 63 | # Edge Case: Robot has a name that needs to be released |
| 64 | if self._name is not None: |
| 65 | Robot._used_names.discard(self._name) |
| 66 | self._name = None |
| 67 | |
| 68 | @classmethod |
| 69 | def _generate_name_pool(cls): |
| 70 | """ |
| 71 | Generate all possible valid names, shuffle them randomly, and store them in the pool. |
| 72 | """ |
| 73 | # Edge Case: Prevent regenerating the pool if it already exists |
| 74 | if cls._name_pool: |
| 75 | return |
| 76 | |
| 77 | letters = string.ascii_uppercase |
| 78 | digits = string.digits |
| 79 | |
| 80 | # Generate all possible combinations |
| 81 | for l1 in letters: |
| 82 | for l2 in letters: |
| 83 | for d1 in digits: |
| 84 | for d2 in digits: |
| 85 | for d3 in digits: |
| 86 | cls._name_pool.append(f"{l1}{l2}{d1}{d2}{d3}") |
| 87 | |
| 88 | # Shuffle the pool randomly |
| 89 | random.shuffle(cls._name_pool) |
| 90 | |
| 91 | # Handled Edge Cases: Robot has no name yet, need to assign one; All possible names have been used; Name pool not yet initialized |
| 92 | # Handled Edge Cases: Robot has a name that needs to be released |
| 93 | # Handled Edge Cases: Prevent regenerating the pool if it already exists |