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