| 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 set to keep track of all names that have been assigned |
| 13 | _used_names = set() |
| 14 | |
| 15 | # Class-level list of all possible names, shuffled for randomness |
| 16 | _name_pool = [] |
| 17 | |
| 18 | # Class-level index to track the next available name from the pool |
| 19 | _next_index = 0 |
| 20 | |
| 2 | 21 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 22 | """ |
| 23 | Initialize a new Robot instance with no name. |
| 24 | """ |
| 25 | # Edge Case: Initialize the name pool if it hasn't been created yet |
| 26 | if not Robot._name_pool: |
| 27 | self._generate_name_pool() |
| 28 | |
| 29 | self._name = None |
| 30 | self.reset() |
| 31 | |
| 5 | 32 | @property |
| 6 | 33 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 34 | """ |
| 35 | Get the robot's current name. |
| 36 | |
| 37 | Returns: |
| 38 | str: The robot's name in the format of two uppercase letters followed by three digits. |
| 39 | """ |
| 40 | return self._name |
| 41 | |
| 9 | 42 | def reset(self) -> None: |
| 10 | | pass |
| 43 | """ |
| 44 | Reset the robot to its factory settings, assigning a new random name. |
| 45 | |
| 46 | The new name is guaranteed to be unique among all robots ever created. |
| 47 | """ |
| 48 | # Edge Case: Release the current name back to the pool of available names |
| 49 | if self._name: |
| 50 | Robot._used_names.discard(self._name) |
| 51 | |
| 52 | # Edge Case: Check if all possible names have been used |
| 53 | if Robot._next_index >= len(Robot._name_pool): |
| 54 | raise RuntimeError("No more unique names available") |
| 55 | |
| 56 | # Assign the next available name from the pool |
| 57 | new_name = Robot._name_pool[Robot._next_index] |
| 58 | Robot._next_index += 1 |
| 59 | |
| 60 | # Edge Case: Ensure the new name hasn't been used (should never happen with our approach) |
| 61 | if new_name in Robot._used_names: |
| 62 | raise RuntimeError("Generated duplicate name") |
| 63 | |
| 64 | # Mark the name as used and assign it to this robot |
| 65 | Robot._used_names.add(new_name) |
| 66 | self._name = new_name |
| 67 | |
| 68 | @classmethod |
| 69 | def _generate_name_pool(cls): |
| 70 | """ |
| 71 | Generate all possible valid robot names, shuffle them randomly, and store them in the pool. |
| 72 | |
| 73 | This method is called once when the first robot is created. |
| 74 | """ |
| 75 | # Generate all possible combinations of two letters and three digits |
| 76 | for letter1 in string.ascii_uppercase: |
| 77 | for letter2 in string.ascii_uppercase: |
| 78 | for digit1 in string.digits: |
| 79 | for digit2 in string.digits: |
| 80 | for digit3 in string.digits: |
| 81 | name = f"{letter1}{letter2}{digit1}{digit2}{digit3}" |
| 82 | cls._name_pool.append(name) |
| 83 | |
| 84 | # Shuffle the pool to ensure randomness |
| 85 | random.shuffle(cls._name_pool) |
| 86 | |
| 87 | # Handled Edge Cases: Initialize name pool if empty, release current name on reset, check for exhausted name pool, ensure name uniqueness |