| 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 | # Class variable to track which names are already assigned |
| 16 | _assigned_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 the name pool if it hasn't been created yet |
| 23 | if not Robot._name_pool: |
| 24 | self._generate_name_pool() |
| 25 | |
| 26 | self._name = None |
| 27 | self.reset() |
| 28 | |
| 5 | 29 | @property |
| 6 | 30 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 31 | """ |
| 32 | Get the robot's current name. |
| 33 | |
| 34 | Returns: |
| 35 | str: The robot's name in the format of two uppercase letters |
| 36 | followed by three digits. |
| 37 | """ |
| 38 | return self._name |
| 39 | |
| 9 | 40 | def reset(self) -> None: |
| 10 | | pass |
| 41 | """ |
| 42 | Reset the robot to its factory settings, assigning a new random name. |
| 43 | |
| 44 | This method removes the robot's current name from the assigned names |
| 45 | set (if it exists) and assigns a new unique name from the pool. |
| 46 | """ |
| 47 | # Edge Case: Return the current name to the pool if it exists |
| 48 | if self._name: |
| 49 | Robot._assigned_names.remove(self._name) |
| 50 | |
| 51 | # Edge Case: Handle the unlikely scenario where all names are assigned |
| 52 | if len(Robot._assigned_names) >= len(Robot._name_pool): |
| 53 | raise RuntimeError("All possible robot names have been assigned.") |
| 54 | |
| 55 | # Assign a new unique name |
| 56 | while True: |
| 57 | new_name = random.choice(Robot._name_pool) |
| 58 | if new_name not in Robot._assigned_names: |
| 59 | self._name = new_name |
| 60 | Robot._assigned_names.add(self._name) |
| 61 | break |
| 62 | |
| 63 | @classmethod |
| 64 | def _generate_name_pool(cls) -> None: |
| 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, then shuffles the |
| 70 | pool to ensure randomness. |
| 71 | """ |
| 72 | # Edge Case: Handle potential memory issues with large name pool |
| 73 | # This is mitigated by the problem's defined format (676,000 names) |
| 74 | |
| 75 | letters = string.ascii_uppercase |
| 76 | digits = string.digits |
| 77 | |
| 78 | # Generate all possible combinations |
| 79 | for first_letter in letters: |
| 80 | for second_letter in letters: |
| 81 | for digit1 in digits: |
| 82 | for digit2 in digits: |
| 83 | for digit3 in digits: |
| 84 | name = f"{first_letter}{second_letter}{digit1}{digit2}{digit3}" |
| 85 | cls._name_pool.append(name) |
| 86 | |
| 87 | # Shuffle the pool to ensure randomness |
| 88 | random.shuffle(cls._name_pool) |
| 89 | |
| 90 | # Handled Edge Cases: |
| 91 | # - Initialize the name pool if it hasn't been created yet |
| 92 | # - Return the current name to the pool during reset |
| 93 | # - Handle the unlikely scenario where all names are assigned |
| 94 | # - Handle potential memory issues with large name pool |
| 95 | |
| 96 | # Handled Edge Cases: |
| 97 | # - Robot initialization with no name |
| 98 | # - Getting the robot's name |
| 99 | # - Resetting the robot's name |
| 100 | # - Returning names to the pool during reset |
| 101 | # - Handling exhaustion of the name pool |