| 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). All names are unique |
| 10 | and randomly assigned from a pre-generated pool. |
| 11 | """ |
| 12 | |
| 13 | # Class variable to hold the pool of all possible names |
| 14 | _name_pool = [] |
| 15 | # Class variable to track used names |
| 16 | _used_names = set() |
| 17 | |
| 2 | 18 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 19 | """ |
| 20 | Initialize a new Robot instance with no name. |
| 21 | """ |
| 22 | # Edge Case: Initialize name pool if it's empty |
| 23 | if not Robot._name_pool: |
| 24 | self._generate_name_pool() |
| 25 | self._name = None |
| 26 | # Edge Case: Handle case when all names are used |
| 27 | if len(Robot._used_names) >= len(Robot._name_pool): |
| 28 | raise RuntimeError("No more unique names available") |
| 29 | self.reset() |
| 30 | |
| 5 | 31 | @property |
| 6 | 32 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 33 | """ |
| 34 | Get the robot's current name. |
| 35 | |
| 36 | Returns: |
| 37 | str: The robot's name. |
| 38 | """ |
| 39 | return self._name |
| 40 | |
| 9 | 41 | def reset(self) -> None: |
| 10 | | pass |
| 42 | """ |
| 43 | Reset the robot to factory settings, assigning a new random name. |
| 44 | |
| 45 | The new name is selected from a pre-generated pool of all possible |
| 46 | valid names, ensuring uniqueness and randomness. |
| 47 | """ |
| 48 | # Edge Case: Remove current name from used names if it exists |
| 49 | if self._name: |
| 50 | Robot._used_names.discard(self._name) |
| 51 | |
| 52 | # Edge Case: Handle case when all names are used |
| 53 | if len(Robot._used_names) >= len(Robot._name_pool): |
| 54 | raise RuntimeError("No more unique names available") |
| 55 | |
| 56 | # Edge Case: Find an unused name from the pool |
| 57 | while True: |
| 58 | candidate = random.choice(Robot._name_pool) |
| 59 | if candidate not in Robot._used_names: |
| 60 | self._name = candidate |
| 61 | Robot._used_names.add(self._name) |
| 62 | break |
| 63 | |
| 64 | @classmethod |
| 65 | def _generate_name_pool(cls) -> None: |
| 66 | """ |
| 67 | Generate all possible valid robot names and shuffle them randomly. |
| 68 | |
| 69 | This method creates a pool of 676,000 unique names in the format |
| 70 | of two uppercase letters followed by three digits. |
| 71 | """ |
| 72 | # Edge Case: Only generate the pool if it's not already populated |
| 73 | if not cls._name_pool: |
| 74 | names = [] |
| 75 | for letter1 in string.ascii_uppercase: |
| 76 | for letter2 in string.ascii_uppercase: |
| 77 | for digit1 in range(10): |
| 78 | for digit2 in range(10): |
| 79 | for digit3 in range(10): |
| 80 | names.append(f"{letter1}{letter2}{digit1}{digit2}{digit3}") |
| 81 | random.shuffle(names) |
| 82 | cls._name_pool = names |
| 83 | |
| 84 | # Handled Edge Cases: Initialize name pool if empty, handle all names used |
| 85 | # Handled Edge Cases: Remove current name from used names, handle all names used, find unused name |
| 86 | # Handled Edge Cases: Only generate pool if not already populated |