| 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 the pool of 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 hasn't been created yet |
| 22 | if not Robot._name_pool: |
| 23 | self._generate_name_pool() |
| 24 | |
| 25 | self._name = None |
| 26 | self.reset() |
| 27 | |
| 5 | 28 | @property |
| 6 | 29 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 30 | """ |
| 31 | Get the robot's current name. |
| 32 | |
| 33 | Returns: |
| 34 | str: The robot's name in the format of two uppercase letters |
| 35 | followed by three digits. |
| 36 | """ |
| 37 | return self._name |
| 38 | |
| 9 | 39 | def reset(self) -> None: |
| 10 | | pass |
| 40 | """ |
| 41 | Reset the robot to its factory settings, assigning a new random name. |
| 42 | |
| 43 | This method removes the robot's current name from the used names set |
| 44 | (if it exists) and assigns a new unique name from the pool. |
| 45 | """ |
| 46 | # Edge Case: Handle the case where the robot already has a name |
| 47 | if self._name: |
| 48 | Robot._used_names.discard(self._name) |
| 49 | |
| 50 | # Edge Case: Handle the case where all possible names have been used |
| 51 | if len(Robot._used_names) >= len(Robot._name_pool): |
| 52 | raise RuntimeError("All possible robot names have been used.") |
| 53 | |
| 54 | # Assign a new name from the pool |
| 55 | while True: |
| 56 | new_name = random.choice(Robot._name_pool) |
| 57 | if new_name not in Robot._used_names: |
| 58 | self._name = new_name |
| 59 | Robot._used_names.add(self._name) |
| 60 | break |
| 61 | |
| 62 | @classmethod |
| 63 | def _generate_name_pool(cls): |
| 64 | """ |
| 65 | Generate all possible valid robot names and shuffle them randomly. |
| 66 | |
| 67 | This method creates a pool of all possible names in the format of |
| 68 | two uppercase letters followed by three digits, then shuffles them |
| 69 | to ensure randomness. |
| 70 | """ |
| 71 | # Edge Case: Handle potential memory issues with large name pool |
| 72 | # This is mitigated by the problem's finite and reasonable name space |
| 73 | |
| 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 | cls._name_pool.append(name) |
| 81 | |
| 82 | random.shuffle(cls._name_pool) |
| 83 | |
| 84 | # Handled Edge Cases: |
| 85 | # 1. Initialize the name pool if it hasn't been created yet |
| 86 | # 2. Handle the case where the robot already has a name |
| 87 | # 3. Handle the case where all possible names have been used |
| 88 | # 4. Handle potential memory issues with large name pool |
| 89 | |
| 90 | # Handled Edge Cases: |
| 91 | # 1. Initialize the name pool if it hasn't been created yet |
| 92 | # 2. Handle the case where the robot already has a name |
| 93 | # 3. Handle the case where all possible names have been used |
| 94 | # 4. Handle potential memory issues with large name pool |