| 1 | import random |
| 2 | import string |
| 3 | |
| 1 | 4 | class Robot: |
| 5 | # Class variable to hold the pool of available names |
| 6 | _name_pool = set() |
| 7 | # Class variable to track all used names |
| 8 | _used_names = set() |
| 9 | |
| 2 | 10 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 11 | # Edge Case: Initialize the name pool if it's empty |
| 12 | if not Robot._name_pool: |
| 13 | self._generate_name_pool() |
| 14 | self._name = None |
| 15 | # Edge Case: Handle the case when all names are used |
| 16 | if not Robot._name_pool: |
| 17 | raise RuntimeError("No more names available") |
| 18 | self._assign_name() |
| 19 | |
| 5 | 20 | @property |
| 6 | 21 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 22 | return self._name |
| 23 | |
| 9 | 24 | def reset(self) -> None: |
| 10 | | pass |
| 25 | # Edge Case: Handle resetting a robot that has a name |
| 26 | if self._name: |
| 27 | # Return the name to the pool |
| 28 | Robot._name_pool.add(self._name) |
| 29 | Robot._used_names.discard(self._name) |
| 30 | # Edge Case: Handle the case when all names are used after many robots were created |
| 31 | if not Robot._name_pool: |
| 32 | raise RuntimeError("No more names available") |
| 33 | self._assign_name() |
| 34 | |
| 35 | def _assign_name(self) -> None: |
| 36 | # Edge Case: Handle the case when trying to assign a name but pool is empty |
| 37 | if not Robot._name_pool: |
| 38 | raise RuntimeError("No more names available") |
| 39 | # Randomly select a name from the pool |
| 40 | new_name = random.choice(list(Robot._name_pool)) |
| 41 | # Remove it from the pool and add to used names |
| 42 | Robot._name_pool.discard(new_name) |
| 43 | Robot._used_names.add(new_name) |
| 44 | self._name = new_name |
| 45 | |
| 46 | @classmethod |
| 47 | def _generate_name_pool(cls) -> None: |
| 48 | # Generate all possible combinations of 2 letters and 3 digits |
| 49 | for letter1 in string.ascii_uppercase: |
| 50 | for letter2 in string.ascii_uppercase: |
| 51 | for digit1 in string.digits: |
| 52 | for digit2 in string.digits: |
| 53 | for digit3 in string.digits: |
| 54 | name = f"{letter1}{letter2}{digit1}{digit2}{digit3}" |
| 55 | cls._name_pool.add(name) |
| 56 | |
| 57 | # Handled Edge Cases: Empty name pool initialization, all names used, resetting robots with names, assigning names when pool is empty |