| 1 | import random |
| 2 | import string |
| 3 | |
| 1 | 4 | class Robot: |
| 5 | # Class variable to hold the name pool and track used names |
| 6 | _name_pool = [] |
| 7 | _used_names = set() |
| 8 | |
| 2 | 9 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 10 | """Initialize a Robot with no name.""" |
| 11 | self._name = None |
| 12 | |
| 5 | 13 | @property |
| 6 | 14 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 15 | """Get the robot's name. Generate one if it doesn't exist.""" |
| 16 | # Edge Case: Robot has no name yet, generate a new one |
| 17 | if self._name is None: |
| 18 | # Edge Case: All possible names have been used |
| 19 | if len(Robot._used_names) >= 676000: # 26*26*10*10*10 |
| 20 | raise RuntimeError("No more unique names available") |
| 21 | |
| 22 | # Generate the name pool if it's empty |
| 23 | if not Robot._name_pool: |
| 24 | self._generate_name_pool() |
| 25 | |
| 26 | # Get a name from the pool |
| 27 | self._name = Robot._name_pool.pop() |
| 28 | Robot._used_names.add(self._name) |
| 29 | |
| 30 | return self._name |
| 31 | |
| 9 | 32 | def reset(self) -> None: |
| 10 | | pass |
| 33 | """Reset the robot to factory settings, wiping its name.""" |
| 34 | # Edge Case: Robot had a name that needs to be returned to the pool |
| 35 | if self._name is not None: |
| 36 | # In this implementation, we don't actually return names to the pool |
| 37 | # as we're using a pre-generated pool and tracking used names separately |
| 38 | Robot._used_names.discard(self._name) |
| 39 | self._name = None |
| 40 | |
| 41 | @classmethod |
| 42 | def _generate_name_pool(cls): |
| 43 | """Generate all possible robot names, shuffle them, and store in the pool.""" |
| 44 | # Edge Case: Prevent regenerating the pool if it already exists |
| 45 | if cls._name_pool: |
| 46 | return |
| 47 | |
| 48 | names = [] |
| 49 | for first_letter in string.ascii_uppercase: |
| 50 | for second_letter in string.ascii_uppercase: |
| 51 | for digit1 in string.digits: |
| 52 | for digit2 in string.digits: |
| 53 | for digit3 in string.digits: |
| 54 | names.append(f"{first_letter}{second_letter}{digit1}{digit2}{digit3}") |
| 55 | |
| 56 | random.shuffle(names) |
| 57 | cls._name_pool = names |
| 58 | |
| 59 | # Handled Edge Cases: Robot has no name yet, all possible names have been used, prevent regenerating the pool |