| 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 followed by three digits. |
| 9 | Each robot has a unique name, and the name can be reset to a new random one. |
| 10 | """ |
| 11 | |
| 12 | # Class-level name pool to ensure uniqueness across all robot instances |
| 13 | _name_pool = [] |
| 14 | _used_names = set() |
| 15 | |
| 2 | 16 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 17 | """ |
| 18 | Initialize a new Robot instance with no name. |
| 19 | The actual name is generated on first access. |
| 20 | """ |
| 21 | self._name = None |
| 22 | |
| 5 | 23 | @property |
| 6 | 24 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 25 | """ |
| 26 | Get the robot's name. If the robot doesn't have a name yet, |
| 27 | generate one from the name pool. |
| 28 | |
| 29 | Returns: |
| 30 | str: The robot's name in format like 'AB123' |
| 31 | """ |
| 32 | # Edge Case: Robot has no name assigned yet |
| 33 | if self._name is None: |
| 34 | # Initialize the name pool if it's empty |
| 35 | # Edge Case: First robot created or pool exhausted |
| 36 | if not Robot._name_pool: |
| 37 | self._generate_name_pool() |
| 38 | |
| 39 | # Assign a name from the pool |
| 40 | self._name = Robot._name_pool.pop() |
| 41 | Robot._used_names.add(self._name) |
| 42 | |
| 43 | return self._name |
| 44 | |
| 9 | 45 | def reset(self) -> None: |
| 10 | | pass |
| 46 | """ |
| 47 | Reset the robot to factory settings, wiping its name. |
| 48 | The next time the name is accessed, a new random name will be generated. |
| 49 | """ |
| 50 | # Edge Case: Resetting a robot that never had a name assigned |
| 51 | if self._name is not None: |
| 52 | # Return the name to the pool for potential reuse |
| 53 | Robot._used_names.discard(self._name) |
| 54 | self._name = None |
| 55 | |
| 56 | @classmethod |
| 57 | def _generate_name_pool(cls): |
| 58 | """ |
| 59 | Generate all possible valid robot names, shuffle them randomly, |
| 60 | and populate the name pool. |
| 61 | """ |
| 62 | # Edge Case: Regenerating pool when it's not empty |
| 63 | if cls._name_pool: |
| 64 | return |
| 65 | |
| 66 | # Generate all possible names in format: two letters + three digits |
| 67 | names = [] |
| 68 | for first_letter in string.ascii_uppercase: |
| 69 | for second_letter in string.ascii_uppercase: |
| 70 | for number in range(1000): |
| 71 | name = f"{first_letter}{second_letter}{number:03d}" |
| 72 | # Only add names that are not currently in use |
| 73 | if name not in cls._used_names: |
| 74 | names.append(name) |
| 75 | |
| 76 | # Shuffle the names randomly |
| 77 | random.shuffle(names) |
| 78 | cls._name_pool = names |
| 79 | |
| 80 | # Handled Edge Cases: Robot has no name assigned yet, First robot created or pool exhausted, |
| 81 | # Resetting a robot that never had a name assigned, Regenerating pool when it's not empty |