| 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 the pool of 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 | """ |
| 23 | self._name = None |
| 24 | |
| 5 | 25 | @property |
| 6 | 26 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 27 | """ |
| 28 | Get the robot's name. If the robot doesn't have a name yet, |
| 29 | assign one from the name pool. |
| 30 | |
| 31 | Returns: |
| 32 | str: The robot's name in format of two uppercase letters followed by three digits. |
| 33 | """ |
| 34 | # Edge Case: Robot has no name assigned yet |
| 35 | if self._name is None: |
| 36 | # Edge Case: All possible names have been used |
| 37 | if len(Robot._used_names) >= 676000: # 26*26*10*10*10 |
| 38 | raise RuntimeError("No more unique names available") |
| 39 | |
| 40 | # Generate the name pool if it hasn't been generated yet |
| 41 | # Edge Case: Name pool is empty (first robot created) |
| 42 | if not Robot._name_pool: |
| 43 | Robot._generate_name_pool() |
| 44 | |
| 45 | # Assign the next available name from the pool |
| 46 | self._name = Robot._name_pool.pop() |
| 47 | Robot._used_names.add(self._name) |
| 48 | |
| 49 | return self._name |
| 50 | |
| 9 | 51 | def reset(self) -> None: |
| 10 | | pass |
| 52 | """ |
| 53 | Reset the robot to its factory settings, wiping its name. |
| 54 | The name is returned to the pool for potential reuse. |
| 55 | """ |
| 56 | # Edge Case: Robot has a name that needs to be returned to the pool |
| 57 | if self._name is not None: |
| 58 | Robot._used_names.discard(self._name) |
| 59 | # For efficiency, we don't actually return the name to the pool |
| 60 | # since we're using a pre-generated pool and tracking used names separately |
| 61 | self._name = None |
| 62 | |
| 63 | @classmethod |
| 64 | def _generate_name_pool(cls): |
| 65 | """ |
| 66 | Generate all possible valid robot names, shuffle them randomly, |
| 67 | and store them in the name pool. |
| 68 | """ |
| 69 | # Edge Case: Generating names when pool already exists |
| 70 | if cls._name_pool: |
| 71 | return |
| 72 | |
| 73 | # Generate all possible names in format of two letters followed by 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 | cls._name_pool.append(first_letter + second_letter + digit1 + digit2 + digit3) |
| 80 | |
| 81 | # Shuffle the pool randomly |
| 82 | random.shuffle(cls._name_pool) |
| 83 | |
| 84 | # Handled Edge Cases: Robot has no name assigned yet, all possible names have been used, |
| 85 | # name pool is empty (first robot created), robot has a name that needs to be returned to the pool, |
| 86 | # generating names when pool already exists |