| 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 when first accessed. |
| 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 yet - generate one |
| 33 | if self._name is None: |
| 34 | # Initialize the name pool if it's empty |
| 35 | # Edge Case: First robot created - generate all possible names |
| 36 | if not Robot._name_pool: |
| 37 | self._generate_name_pool() |
| 38 | |
| 39 | # Edge Case: All possible names have been used - raise an exception |
| 40 | if not Robot._name_pool: |
| 41 | raise RuntimeError("No more unique names available") |
| 42 | |
| 43 | # Assign the next available name from the pool |
| 44 | self._name = Robot._name_pool.pop() |
| 45 | Robot._used_names.add(self._name) |
| 46 | |
| 47 | return self._name |
| 48 | |
| 9 | 49 | def reset(self) -> None: |
| 10 | | pass |
| 50 | """ |
| 51 | Reset the robot to factory settings, wiping its name. |
| 52 | The next time the name is accessed, a new random name will be generated. |
| 53 | """ |
| 54 | # Edge Case: Robot had a name that needs to be returned to the pool |
| 55 | if self._name is not None: |
| 56 | # In a real implementation with a proper pool, we might return the name |
| 57 | # to the pool. However, with the current approach of consuming names |
| 58 | # from a shuffled list, we simply release the name and clear it. |
| 59 | Robot._used_names.discard(self._name) |
| 60 | self._name = None |
| 61 | |
| 62 | @classmethod |
| 63 | def _generate_name_pool(cls): |
| 64 | """ |
| 65 | Generate all possible valid robot names, shuffle them randomly, |
| 66 | and store them in the class-level name pool. |
| 67 | """ |
| 68 | # Edge Case: Prevent regenerating the pool if it already exists |
| 69 | if cls._name_pool: |
| 70 | return |
| 71 | |
| 72 | names = [] |
| 73 | # Generate all combinations of two letters and three digits |
| 74 | for first_letter in string.ascii_uppercase: |
| 75 | for second_letter in string.ascii_uppercase: |
| 76 | for digit1 in string.digits: |
| 77 | for digit2 in string.digits: |
| 78 | for digit3 in string.digits: |
| 79 | name = f"{first_letter}{second_letter}{digit1}{digit2}{digit3}" |
| 80 | # Edge Case: Ensure we don't add names that are already used |
| 81 | if name not in cls._used_names: |
| 82 | names.append(name) |
| 83 | |
| 84 | # Shuffle the names randomly |
| 85 | random.shuffle(names) |
| 86 | cls._name_pool = names |
| 87 | |
| 88 | # Handled Edge Cases: Robot has no name yet, First robot created, All possible names have been used, Robot had a name that needs to be returned to the pool, Prevent regenerating the pool if it already exists |