| 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). Names are unique and |
| 10 | randomly assigned from a pre-generated pool. |
| 11 | """ |
| 12 | |
| 13 | # Class variable to hold all possible names |
| 14 | _name_pool = [] |
| 15 | _used_names = set() |
| 16 | |
| 2 | 17 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 18 | """ |
| 19 | Initialize a new Robot instance with no name. |
| 20 | """ |
| 21 | # Edge Case: Initialize the name pool if it's empty |
| 22 | if not Robot._name_pool: |
| 23 | self._generate_name_pool() |
| 24 | self._name = None |
| 25 | self._assign_name() |
| 26 | |
| 5 | 27 | @property |
| 6 | 28 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 29 | """ |
| 30 | Get the robot's current name. |
| 31 | |
| 32 | Returns: |
| 33 | str: The robot's name in format of two uppercase letters followed by three digits. |
| 34 | """ |
| 35 | return self._name |
| 36 | |
| 9 | 37 | def reset(self) -> None: |
| 10 | | pass |
| 38 | """ |
| 39 | Reset the robot to factory settings, wiping its name. |
| 40 | |
| 41 | The robot will receive a new unique random name when next accessed. |
| 42 | """ |
| 43 | # Edge Case: Handle resetting a robot that might not have a name |
| 44 | if self._name: |
| 45 | Robot._used_names.discard(self._name) |
| 46 | self._assign_name() |
| 47 | |
| 48 | def _assign_name(self) -> None: |
| 49 | """ |
| 50 | Assign a new unique name to the robot from the name pool. |
| 51 | """ |
| 52 | # Edge Case: Handle the unlikely scenario where all names are used |
| 53 | if len(Robot._used_names) >= len(Robot._name_pool): |
| 54 | raise RuntimeError("All possible robot names have been used.") |
| 55 | |
| 56 | # Keep trying until we find an unused name |
| 57 | while True: |
| 58 | candidate = random.choice(Robot._name_pool) |
| 59 | if candidate not in Robot._used_names: |
| 60 | self._name = candidate |
| 61 | Robot._used_names.add(candidate) |
| 62 | break |
| 63 | |
| 64 | @classmethod |
| 65 | def _generate_name_pool(cls) -> None: |
| 66 | """ |
| 67 | Generate all possible valid robot names and shuffle them randomly. |
| 68 | |
| 69 | This method creates names in the format of two uppercase letters |
| 70 | followed by three digits, then shuffles them to ensure randomness. |
| 71 | """ |
| 72 | # Edge Case: Prevent regenerating the pool if it already exists |
| 73 | if cls._name_pool: |
| 74 | return |
| 75 | |
| 76 | names = [] |
| 77 | # Generate all combinations of two letters and three digits |
| 78 | for first_letter in string.ascii_uppercase: |
| 79 | for second_letter in string.ascii_uppercase: |
| 80 | for number in range(1000): |
| 81 | name = f"{first_letter}{second_letter}{number:03d}" |
| 82 | names.append(name) |
| 83 | |
| 84 | # Shuffle the names to ensure randomness |
| 85 | random.shuffle(names) |
| 86 | cls._name_pool = names |
| 87 | |
| 88 | # Handled Edge Cases: Initialize the name pool if it's empty, |
| 89 | # Handle resetting a robot that might not have a name, |
| 90 | # Handle the unlikely scenario where all names are used, |
| 91 | # Prevent regenerating the pool if it already exists |