| 1 | import random |
| 2 | import string |
| 3 | |
| 4 | |
| 1 | 5 | class Robot: |
| 6 | _used_names = set() |
| 7 | |
| 2 | 8 | def __init__(self): |
| 3 | | pass |
| 9 | self._name = None |
| 10 | self._random = random.Random() # Each robot has its own random instance |
| 11 | self.reset() |
| 12 | |
| 13 | @property |
| 14 | def name(self): |
| 15 | return self._name |
| 16 | |
| 17 | def reset(self): |
| 18 | # Edge Case: Clear the current name from used names if it exists |
| 19 | if self._name: |
| 20 | self._used_names.discard(self._name) |
| 21 | |
| 22 | # Edge Case: Handle name collision by generating new names until we find an unused one |
| 23 | while True: |
| 24 | # Generate two random uppercase letters |
| 25 | letters = ''.join(self._random.choices(string.ascii_uppercase, k=2)) |
| 26 | # Generate three random digits |
| 27 | digits = ''.join(self._random.choices(string.digits, k=3)) |
| 28 | # Combine to form the name |
| 29 | new_name = letters + digits |
| 30 | |
| 31 | # Edge Case: Check if the generated name is already used |
| 32 | if new_name not in self._used_names: |
| 33 | self._name = new_name |
| 34 | self._used_names.add(new_name) |
| 35 | break |
| 36 | |
| 37 | # Edge Case: Theoretically handle exhaustion of name space (though practically impossible with 676,000 combinations) |
| 38 | # This is handled by the while loop which will continue until a unique name is found |
| 39 | |
| 40 | # Handled Edge Cases: name collision handling, exhaustion of available name space, persistence of assigned names across resets |