| 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 selected from a pre-generated pool. |
| 11 | """ |
| 12 | |
| 13 | # Class variable to hold all possible names |
| 14 | _name_pool = None |
| 15 | |
| 2 | 16 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 17 | """ |
| 18 | Initialize a new Robot instance. |
| 19 | |
| 20 | The robot starts without a name. A name will be assigned when |
| 21 | the name property is first accessed. |
| 22 | """ |
| 23 | self._assigned_name = None |
| 24 | |
| 5 | 25 | @property |
| 6 | 26 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 27 | """ |
| 28 | Get the robot's name. |
| 29 | |
| 30 | If the robot doesn't have a name yet, assign one randomly from |
| 31 | the name pool. If the pool is exhausted, raise an exception. |
| 32 | |
| 33 | Returns: |
| 34 | str: The robot's name in the format of two uppercase letters |
| 35 | followed by three digits. |
| 36 | |
| 37 | Raises: |
| 38 | RuntimeError: If all possible names have been exhausted. |
| 39 | """ |
| 40 | # Edge Case: Robot already has a name, return it |
| 41 | if self._assigned_name is not None: |
| 42 | return self._assigned_name |
| 43 | |
| 44 | # Edge Case: First access to name, initialize the name pool if needed |
| 45 | if Robot._name_pool is None: |
| 46 | Robot._generate_name_pool() |
| 47 | |
| 48 | # Edge Case: Name pool is empty (all names have been used) |
| 49 | if not Robot._name_pool: |
| 50 | raise RuntimeError("No more names available") |
| 51 | |
| 52 | # Assign a random name from the pool |
| 53 | self._assigned_name = Robot._name_pool.pop() |
| 54 | return self._assigned_name |
| 55 | |
| 9 | 56 | def reset(self) -> None: |
| 10 | | pass |
| 57 | """ |
| 58 | Reset the robot to its factory settings. |
| 59 | |
| 60 | This method wipes the robot's current name. The next time the |
| 61 | name property is accessed, a new random name will be assigned. |
| 62 | """ |
| 63 | # Edge Case: Robot has a name that needs to be returned to the pool |
| 64 | if self._assigned_name is not None: |
| 65 | # Add the name back to the pool for potential reuse |
| 66 | if Robot._name_pool is not None: |
| 67 | Robot._name_pool.add(self._assigned_name) |
| 68 | |
| 69 | # Clear the robot's assigned name |
| 70 | self._assigned_name = None |
| 71 | |
| 72 | @classmethod |
| 73 | def _generate_name_pool(cls): |
| 74 | """ |
| 75 | Generate all possible valid robot names and store them in a set. |
| 76 | |
| 77 | This method creates names in the format of two uppercase letters |
| 78 | followed by three digits. All names are stored in a class-level set. |
| 79 | """ |
| 80 | # Edge Case: Name pool already exists, do nothing |
| 81 | if cls._name_pool is not None: |
| 82 | return |
| 83 | |
| 84 | cls._name_pool = set() |
| 85 | for letter1 in string.ascii_uppercase: |
| 86 | for letter2 in string.ascii_uppercase: |
| 87 | for digit1 in string.digits: |
| 88 | for digit2 in string.digits: |
| 89 | for digit3 in string.digits: |
| 90 | name = f"{letter1}{letter2}{digit1}{digit2}{digit3}" |
| 91 | cls._name_pool.add(name) |
| 92 | |
| 93 | # Handled Edge Cases: Robot already has a name, first access to name, name pool is empty, name pool already exists |
| 94 | |
| 95 | # Handled Edge Cases: Robot already has a name, first access to name, name pool is empty, robot has a name that needs to be returned to the pool, name pool already exists |