| 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 and track used names |
| 14 | _name_pool = set() |
| 15 | _used_names = set() |
| 16 | |
| 17 | # Generate all possible names once at class initialization |
| 18 | # Edge Case: Pre-generate all possible valid names to ensure uniqueness |
| 19 | for letter1 in string.ascii_uppercase: |
| 20 | for letter2 in string.ascii_uppercase: |
| 21 | for digit1 in string.digits: |
| 22 | for digit2 in string.digits: |
| 23 | for digit3 in string.digits: |
| 24 | _name_pool.add(f"{letter1}{letter2}{digit1}{digit2}{digit3}") |
| 25 | |
| 2 | 26 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 27 | """ |
| 28 | Initialize a new robot with no name. |
| 29 | """ |
| 30 | self._name = None |
| 31 | |
| 5 | 32 | @property |
| 6 | 33 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 34 | """ |
| 35 | Get the robot's name. If the robot doesn't have a name yet, |
| 36 | assign a random unique name from the pool. |
| 37 | |
| 38 | Returns: |
| 39 | str: The robot's name in format of two uppercase letters followed by three digits. |
| 40 | """ |
| 41 | # Edge Case: Robot has no name, assign a new one |
| 42 | if self._name is None: |
| 43 | # Edge Case: No names left in the pool |
| 44 | if len(Robot._name_pool) <= len(Robot._used_names): |
| 45 | raise RuntimeError("No more names available") |
| 46 | |
| 47 | # Select a name that hasn't been used |
| 48 | available_names = Robot._name_pool - Robot._used_names |
| 49 | self._name = random.choice(list(available_names)) |
| 50 | Robot._used_names.add(self._name) |
| 51 | |
| 52 | return self._name |
| 53 | |
| 9 | 54 | def reset(self) -> None: |
| 10 | | pass |
| 55 | """ |
| 56 | Reset the robot to factory settings, wiping its name. |
| 57 | The name is returned to the pool of available names. |
| 58 | """ |
| 59 | # Edge Case: Reset robot that has a name, return name to pool |
| 60 | if self._name is not None: |
| 61 | Robot._used_names.discard(self._name) |
| 62 | self._name = None |
| 63 | |
| 64 | # Handled Edge Cases: Robot has no name, assign a new one; No names left in the pool; Reset robot that has a name, return name to pool |