| 1 | import random |
| 2 | import string |
| 3 | |
| 1 | 4 | class Robot: |
| 5 | """ |
| 6 | A class representing a robot with a randomly generated name. |
| 7 | |
| 8 | The robot's name consists of two uppercase letters followed by three digits. |
| 9 | Each robot has a unique name across all robots ever created. |
| 10 | """ |
| 11 | # Class variable to keep track of all names ever assigned |
| 12 | # Edge Case: Maintain a persistent global registry of all names ever assigned to ensure uniqueness |
| 13 | _used_names = set() |
| 14 | |
| 2 | 15 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 16 | """ |
| 17 | Initialize a new Robot instance with no name. |
| 18 | The actual name is generated on first access. |
| 19 | """ |
| 20 | self._name = None |
| 21 | |
| 5 | 22 | @property |
| 6 | 23 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 24 | """ |
| 25 | Get the robot's name. If the robot doesn't have a name yet, |
| 26 | generate a new unique random name. |
| 27 | |
| 28 | Returns: |
| 29 | str: The robot's name in format like 'RX837' or 'BC811'. |
| 30 | |
| 31 | Raises: |
| 32 | RuntimeError: If all possible names are exhausted. |
| 33 | """ |
| 34 | # Edge Case: Generate name on first access |
| 35 | if self._name is None: |
| 36 | self._generate_name() |
| 37 | return self._name |
| 38 | |
| 9 | 39 | def reset(self) -> None: |
| 10 | | pass |
| 40 | """ |
| 41 | Reset the robot to its factory settings, wiping its name. |
| 42 | The next access to the name property will generate a new unique name. |
| 43 | """ |
| 44 | # Edge Case: Remove old name from used names set before resetting |
| 45 | if self._name is not None: |
| 46 | Robot._used_names.discard(self._name) |
| 47 | self._name = None |
| 48 | |
| 49 | def _generate_name(self) -> None: |
| 50 | """ |
| 51 | Generate a new unique random name for the robot using cryptographically secure randomness. |
| 52 | |
| 53 | Raises: |
| 54 | RuntimeError: If all possible names are exhausted. |
| 55 | """ |
| 56 | # Edge Case: Handle name exhaustion |
| 57 | max_attempts = 676000 # 26*26*10*10*10 = 676,000 possible names |
| 58 | attempts = 0 |
| 59 | |
| 60 | while attempts < max_attempts: |
| 61 | # Edge Case: Use cryptographically secure random number generation |
| 62 | # Generate two random uppercase letters |
| 63 | letters = ''.join(random.SystemRandom().choices(string.ascii_uppercase, k=2)) |
| 64 | # Generate three random digits |
| 65 | digits = ''.join(random.SystemRandom().choices(string.digits, k=3)) |
| 66 | # Combine to form the name |
| 67 | new_name = letters + digits |
| 68 | |
| 69 | # Edge Case: Check for uniqueness |
| 70 | if new_name not in Robot._used_names: |
| 71 | Robot._used_names.add(new_name) |
| 72 | self._name = new_name |
| 73 | return |
| 74 | |
| 75 | attempts += 1 |
| 76 | |
| 77 | # Edge Case: All possible names are exhausted |
| 78 | raise RuntimeError("All possible robot names have been exhausted") |
| 79 | |
| 80 | # Handled Edge Cases: Generate name on first access, Handle name exhaustion, Check for uniqueness, All possible names are exhausted, Use cryptographically secure random number generation, Maintain a persistent global registry of all names ever assigned |