| 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). All names are unique |
| 10 | and randomly assigned from a pre-generated pool. |
| 11 | """ |
| 12 | |
| 13 | # Class variable to hold the pool of all possible names |
| 14 | _name_pool = [] |
| 15 | # Class variable to track used names |
| 16 | _used_names = set() |
| 17 | |
| 2 | 18 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 19 | """ |
| 20 | Initialize a new Robot instance with no name. |
| 21 | """ |
| 22 | # Edge Case: Initialize name pool if it's empty |
| 23 | if not Robot._name_pool: |
| 24 | self._generate_name_pool() |
| 25 | self._name = None |
| 26 | |
| 27 | def _generate_name_pool(self): |
| 28 | """ |
| 29 | Generate all possible valid names and shuffle them randomly. |
| 30 | """ |
| 31 | # Edge Case: Handle potential memory issues with large name pool |
| 32 | # This is mitigated by the fixed size of the name space (26*26*10*10*10 = 676,000 names) |
| 33 | for letter1 in string.ascii_uppercase: |
| 34 | for letter2 in string.ascii_uppercase: |
| 35 | for digit1 in string.digits: |
| 36 | for digit2 in string.digits: |
| 37 | for digit3 in string.digits: |
| 38 | name = f"{letter1}{letter2}{digit1}{digit2}{digit3}" |
| 39 | Robot._name_pool.append(name) |
| 40 | random.shuffle(Robot._name_pool) |
| 41 | |
| 5 | 42 | @property |
| 6 | 43 | def name(self) -> str: |
| 7 | | pass |
| 44 | """ |
| 45 | Get the robot's name. If the robot doesn't have a name yet, |
| 46 | assign one from the name pool. |
| 47 | |
| 48 | Returns: |
| 49 | str: The robot's name in format of two uppercase letters followed by three digits. |
| 50 | """ |
| 51 | # Edge Case: Robot already has a name, return it |
| 52 | if self._name is not None: |
| 53 | return self._name |
| 54 | |
| 55 | # Edge Case: No more names available in the pool |
| 56 | if not Robot._name_pool: |
| 57 | raise RuntimeError("No more unique names available") |
| 58 | |
| 59 | # Assign a name from the pool |
| 60 | self._name = Robot._name_pool.pop() |
| 61 | Robot._used_names.add(self._name) |
| 62 | return self._name |
| 8 | 63 | |
| 9 | 64 | def reset(self) -> None: |
| 10 | | pass |
| 65 | """ |
| 66 | Reset the robot to its factory settings, wiping its name. |
| 67 | The name is returned to the pool for potential reuse. |
| 68 | """ |
| 69 | # Edge Case: Robot has no name to reset |
| 70 | if self._name is not None: |
| 71 | # Return the name to the pool for potential reuse |
| 72 | Robot._used_names.discard(self._name) |
| 73 | # We don't actually put it back in the pool to maintain uniqueness |
| 74 | # as required by the problem statement |
| 75 | self._name = None |
| 76 | |
| 77 | # Handled Edge Cases: Robot already has a name, no more names available, robot has no name to reset |