| 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 | The actual name is generated when first accessed. |
| 22 | """ |
| 23 | self._assigned_name = None |
| 24 | # Generate the name pool if it hasn't been generated yet |
| 25 | if not Robot._name_pool: |
| 26 | self._generate_name_pool() |
| 27 | |
| 5 | 28 | @property |
| 6 | 29 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 30 | """ |
| 31 | Get the robot's name. If the robot doesn't have a name yet, |
| 32 | assign one from the name pool. |
| 33 | |
| 34 | Returns: |
| 35 | str: The robot's name in format of two uppercase letters followed by three digits. |
| 36 | """ |
| 37 | # Edge Case: Robot already has a name assigned |
| 38 | if self._assigned_name is not None: |
| 39 | return self._assigned_name |
| 40 | |
| 41 | # Edge Case: All possible names have been used |
| 42 | if len(Robot._used_names) >= len(Robot._name_pool): |
| 43 | raise RuntimeError("No more unique names available") |
| 44 | |
| 45 | # Assign a name from the pool that hasn't been used yet |
| 46 | while True: |
| 47 | candidate_name = random.choice(Robot._name_pool) |
| 48 | if candidate_name not in Robot._used_names: |
| 49 | self._assigned_name = candidate_name |
| 50 | Robot._used_names.add(candidate_name) |
| 51 | break |
| 52 | |
| 53 | return self._assigned_name |
| 54 | |
| 9 | 55 | def reset(self) -> None: |
| 10 | | pass |
| 56 | """ |
| 57 | Reset the robot to factory settings, wiping its name. |
| 58 | The name is returned to the pool of available names. |
| 59 | """ |
| 60 | # Edge Case: Robot has no name to reset |
| 61 | if self._assigned_name is not None: |
| 62 | Robot._used_names.discard(self._assigned_name) |
| 63 | self._assigned_name = None |
| 64 | |
| 65 | @classmethod |
| 66 | def _generate_name_pool(cls): |
| 67 | """ |
| 68 | Generate all possible valid robot names and shuffle them randomly. |
| 69 | This method is called once to initialize the name pool. |
| 70 | """ |
| 71 | # Edge Case: Name pool already generated |
| 72 | if cls._name_pool: |
| 73 | return |
| 74 | |
| 75 | # Generate all possible combinations of 2 letters + 3 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 | cls._name_pool.append(f"{letter1}{letter2}{digit1}{digit2}{digit3}") |
| 82 | |
| 83 | # Shuffle the pool randomly |
| 84 | random.shuffle(cls._name_pool) |
| 85 | |
| 86 | # Handled Edge Cases: Robot already has a name assigned, All possible names have been used |
| 87 | # Handled Edge Cases: Robot has no name to reset, Name pool already generated |