| 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 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 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. |
| 36 | """ |
| 37 | return self._name |
| 38 | |
| 9 | 39 | def reset(self) -> None: |
| 10 | | pass |
| 40 | """ |
| 41 | Reset the robot to factory settings, wiping its name and assigning a new one. |
| 42 | """ |
| 43 | # Edge Case: Release the current name back to the pool if it exists |
| 44 | if self._name: |
| 45 | Robot._used_names.discard(self._name) |
| 46 | |
| 47 | # Edge Case: Check if all possible names have been used |
| 48 | if len(Robot._used_names) >= len(Robot._name_pool): |
| 49 | raise RuntimeError("No more unique names available") |
| 50 | |
| 51 | # Assign a new name from the pool |
| 52 | while True: |
| 53 | candidate_name = random.choice(Robot._name_pool) |
| 54 | if candidate_name not in Robot._used_names: |
| 55 | self._name = candidate_name |
| 56 | Robot._used_names.add(self._name) |
| 57 | break |
| 58 | |
| 59 | @classmethod |
| 60 | def _generate_name_pool(cls): |
| 61 | """ |
| 62 | Generate all possible valid robot names and shuffle them randomly. |
| 63 | |
| 64 | This method creates a pool of all possible names in the format of |
| 65 | two uppercase letters followed by three digits. |
| 66 | """ |
| 67 | # Edge Case: Only generate the pool if it hasn't been generated yet |
| 68 | if cls._name_pool: |
| 69 | return |
| 70 | |
| 71 | names = [] |
| 72 | for letter1 in string.ascii_uppercase: |
| 73 | for letter2 in string.ascii_uppercase: |
| 74 | for digit1 in range(10): |
| 75 | for digit2 in range(10): |
| 76 | for digit3 in range(10): |
| 77 | name = f"{letter1}{letter2}{digit1}{digit2}{digit3}" |
| 78 | names.append(name) |
| 79 | |
| 80 | # Shuffle the names to ensure randomness |
| 81 | random.shuffle(names) |
| 82 | cls._name_pool = names |
| 83 | |
| 84 | # Handled Edge Cases: Initialize name pool if empty, release current name back to pool, |
| 85 | # check for exhausted name pool, prevent duplicate name assignment, |
| 86 | # only generate name pool once |