| 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 consists of two uppercase letters followed by three digits. |
| 9 | Names are unique and randomly assigned upon first access. |
| 10 | The robot can be reset to generate a new name. |
| 11 | """ |
| 12 | |
| 13 | # Class-level set to track all used names across all robot instances |
| 14 | _used_names = set() |
| 15 | # Class-level list of all possible names, shuffled for randomness |
| 16 | _name_pool = [] |
| 17 | # Class-level index to track the next available name in the pool |
| 18 | _next_index = 0 |
| 19 | |
| 2 | 20 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 21 | """ |
| 22 | Initialize a new Robot instance with no name. |
| 23 | The name will be generated on first access. |
| 24 | """ |
| 25 | self._name = None |
| 26 | |
| 5 | 27 | @property |
| 6 | 28 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 29 | """ |
| 30 | Get the robot's name. If the robot doesn't have a name yet, |
| 31 | generate a new unique random name. |
| 32 | |
| 33 | Returns: |
| 34 | str: The robot's name in format AA000 |
| 35 | """ |
| 36 | # Edge Case: Robot has no name yet, need to assign one |
| 37 | if self._name is None: |
| 38 | # Edge Case: All possible names have been used |
| 39 | if len(Robot._used_names) >= 26*26*1000: # 676000 possible names |
| 40 | raise RuntimeError("No more unique names available") |
| 41 | |
| 42 | # Edge Case: Name pool hasn't been initialized yet |
| 43 | if not Robot._name_pool: |
| 44 | self._generate_name_pool() |
| 45 | |
| 46 | # Find an unused name from the pool |
| 47 | while Robot._next_index < len(Robot._name_pool): |
| 48 | candidate_name = Robot._name_pool[Robot._next_index] |
| 49 | Robot._next_index += 1 |
| 50 | |
| 51 | # Edge Case: Found an unused name in the pool |
| 52 | if candidate_name not in Robot._used_names: |
| 53 | self._name = candidate_name |
| 54 | Robot._used_names.add(self._name) |
| 55 | break |
| 56 | |
| 57 | # Edge Case: This should not happen if the pool is correctly managed |
| 58 | if self._name is None: |
| 59 | raise RuntimeError("Failed to assign a name") |
| 60 | |
| 61 | return self._name |
| 62 | |
| 9 | 63 | def reset(self) -> None: |
| 10 | | pass |
| 64 | """ |
| 65 | Reset the robot to factory settings, wiping its name. |
| 66 | The next time the name is accessed, a new unique random name will be generated. |
| 67 | """ |
| 68 | # Edge Case: Robot has a name that needs to be released |
| 69 | if self._name is not None: |
| 70 | Robot._used_names.discard(self._name) |
| 71 | self._name = None |
| 72 | |
| 73 | @classmethod |
| 74 | def _generate_name_pool(cls): |
| 75 | """ |
| 76 | Generate all possible robot names and shuffle them for randomness. |
| 77 | This is done once for the entire class. |
| 78 | """ |
| 79 | # Edge Case: Prevent multiple initializations of the name pool |
| 80 | if cls._name_pool: |
| 81 | return |
| 82 | |
| 83 | # Generate all possible names: two letters + three digits |
| 84 | for first_letter in string.ascii_uppercase: |
| 85 | for second_letter in string.ascii_uppercase: |
| 86 | for number in range(1000): |
| 87 | name = f"{first_letter}{second_letter}{number:03d}" |
| 88 | cls._name_pool.append(name) |
| 89 | |
| 90 | # Shuffle the pool to ensure randomness |
| 91 | random.shuffle(cls._name_pool) |
| 92 | |
| 93 | # Handled Edge Cases: Robot has no name yet, need to assign one; All possible names have been used; Name pool hasn't been initialized yet; Found an unused name in the pool; This should not happen if the pool is correctly managed; Robot has a name that needs to be released; Prevent multiple initializations of the name pool |