| 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 name format is two uppercase letters followed by three digits (e.g., RX837). |
| 9 | Each robot has a unique name, and the name can be reset to generate a new one. |
| 10 | """ |
| 11 | |
| 12 | # Class-level set to track all used names across all robot instances |
| 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 random unique name. |
| 27 | |
| 28 | Returns: |
| 29 | str: The robot's name in format AA000. |
| 30 | """ |
| 31 | # Edge Case: Name not yet generated - generate a new unique name |
| 32 | if self._name is None: |
| 33 | self._generate_unique_name() |
| 34 | return self._name |
| 35 | |
| 9 | 36 | def reset(self) -> None: |
| 10 | | pass |
| 37 | """ |
| 38 | Reset the robot to factory settings by wiping its name. |
| 39 | The next access to the name property will generate a new unique name. |
| 40 | """ |
| 41 | # Edge Case: Robot has a name that needs to be released from the used names set |
| 42 | # This implements the 'wiped' name requirement by removing from used names |
| 43 | if self._name is not None: |
| 44 | Robot._used_names.discard(self._name) |
| 45 | # Edge Case: What defines a 'wiped' name - setting the name attribute to None |
| 46 | self._name = None |
| 47 | |
| 48 | def _generate_unique_name(self) -> None: |
| 49 | """ |
| 50 | Generate a unique random name for the robot. |
| 51 | |
| 52 | The name format is two uppercase letters followed by three digits. |
| 53 | This method ensures the generated name is unique across all robot instances. |
| 54 | """ |
| 55 | # Edge Case: All possible names are exhausted (676,000 total combinations) |
| 56 | # When exhausted, we reuse names from decommissioned robots |
| 57 | if len(Robot._used_names) >= 26 * 26 * 10 * 10 * 10: |
| 58 | # Find all possible names that are not currently in use |
| 59 | # This is a simplified approach - in practice, we might maintain a separate pool |
| 60 | raise RuntimeError("All possible robot names have been exhausted") |
| 61 | |
| 62 | # Edge Case: High collision rate causing many attempts to find unique name |
| 63 | # Edge Case: Insufficient randomness leading to predictable sequences |
| 64 | # Using SystemRandom for cryptographically secure random generation |
| 65 | max_attempts = 1000 |
| 66 | attempts = 0 |
| 67 | |
| 68 | while attempts < max_attempts: |
| 69 | # Generate two random uppercase letters using cryptographically secure random |
| 70 | letters = ''.join(random.SystemRandom().choices(string.ascii_uppercase, k=2)) |
| 71 | # Generate three random digits using cryptographically secure random |
| 72 | digits = ''.join(random.SystemRandom().choices(string.digits, k=3)) |
| 73 | # Combine to form the name |
| 74 | new_name = letters + digits |
| 75 | |
| 76 | # Check if this name is unique |
| 77 | if new_name not in Robot._used_names: |
| 78 | Robot._used_names.add(new_name) |
| 79 | self._name = new_name |
| 80 | return |
| 81 | |
| 82 | attempts += 1 |
| 83 | |
| 84 | # This should practically never happen with good randomness |
| 85 | raise RuntimeError("Unable to generate a unique robot name after maximum attempts") |
| 86 | |
| 87 | # Handled Edge Cases: Name not yet generated, robot has a name that needs to be released, what defines a 'wiped' name, all possible names exhausted, high collision rate, insufficient randomness leading to predictable sequences |