| 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 the pool of 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 hasn't been created yet |
| 22 | if not Robot._name_pool: |
| 23 | self._generate_name_pool() |
| 24 | self._name = None |
| 25 | |
| 26 | def _generate_name_pool(self): |
| 27 | """ |
| 28 | Generate all possible valid robot names and shuffle them randomly. |
| 29 | """ |
| 30 | # Edge Case: Handle potential memory issues with large name pool |
| 31 | # This is mitigated by the fixed size of the pool (676,000 names) |
| 32 | names = [] |
| 33 | for first_letter in string.ascii_uppercase: |
| 34 | for second_letter in string.ascii_uppercase: |
| 35 | for number in range(1000): |
| 36 | names.append(f"{first_letter}{second_letter}{number:03d}") |
| 37 | random.shuffle(names) |
| 38 | Robot._name_pool = names |
| 39 | |
| 5 | 40 | @property |
| 6 | 41 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 42 | """ |
| 43 | Get the robot's name. If the robot doesn't have a name yet, |
| 44 | assign a random one from the pool. |
| 45 | |
| 46 | Returns: |
| 47 | str: The robot's name in format XX### |
| 48 | """ |
| 49 | # Edge Case: Assign a name if the robot doesn't have one |
| 50 | if self._name is None: |
| 51 | # Edge Case: Handle exhaustion of the name pool |
| 52 | if not Robot._name_pool: |
| 53 | raise RuntimeError("No more names available") |
| 54 | |
| 55 | self._name = Robot._name_pool.pop() |
| 56 | Robot._used_names.add(self._name) |
| 57 | return self._name |
| 58 | |
| 9 | 59 | def reset(self) -> None: |
| 10 | | pass |
| 60 | """ |
| 61 | Reset the robot to factory settings, wiping its name. |
| 62 | The name is returned to the pool for potential reuse. |
| 63 | """ |
| 64 | # Edge Case: Handle reset when robot has no name |
| 65 | if self._name is not None: |
| 66 | # Return the name to the pool |
| 67 | Robot._used_names.discard(self._name) |
| 68 | # In a real implementation, we might want to add it back to the pool |
| 69 | # For this exercise, we'll just mark it as unused |
| 70 | self._name = None |
| 71 | |
| 72 | # Handled Edge Cases: Initialize the name pool if it hasn't been created yet, |
| 73 | # Handle potential memory issues with large name pool, Assign a name if the robot doesn't have one, |
| 74 | # Handle exhaustion of the name pool, Handle reset when robot has no name |