| 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 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._assign_name() |
| 29 | |
| 5 | 30 | @property |
| 6 | 31 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 32 | """ |
| 33 | Get the robot's current 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 its factory settings, wiping its name. |
| 43 | A new random name will be assigned the next time it's requested. |
| 44 | """ |
| 45 | # Edge Case: Handle resetting a robot that has a name |
| 46 | if self._name: |
| 47 | Robot._used_names.discard(self._name) |
| 48 | # Edge Case: Handle the case where all names are used |
| 49 | if len(Robot._used_names) >= len(Robot._name_pool): |
| 50 | raise RuntimeError("All possible robot names have been used.") |
| 51 | self._assign_name() |
| 52 | |
| 53 | def _assign_name(self) -> None: |
| 54 | """ |
| 55 | Assign a unique name to the robot from the name pool. |
| 56 | """ |
| 57 | # Edge Case: Handle name assignment when name pool is exhausted |
| 58 | if len(Robot._used_names) >= len(Robot._name_pool): |
| 59 | raise RuntimeError("All possible robot names have been used.") |
| 60 | |
| 61 | while True: |
| 62 | name = random.choice(Robot._name_pool) |
| 63 | if name not in Robot._used_names: |
| 64 | self._name = name |
| 65 | Robot._used_names.add(name) |
| 66 | break |
| 67 | |
| 68 | @classmethod |
| 69 | def _generate_name_pool(cls) -> None: |
| 70 | """ |
| 71 | Generate all possible robot names and shuffle them randomly. |
| 72 | """ |
| 73 | # Edge Case: Handle name pool generation when it's already populated |
| 74 | if cls._name_pool: |
| 75 | return |
| 76 | |
| 77 | names = [] |
| 78 | # Generate all combinations of two letters |
| 79 | for first_letter in string.ascii_uppercase: |
| 80 | for second_letter in string.ascii_uppercase: |
| 81 | # Generate all combinations of three digits |
| 82 | for i in range(1000): |
| 83 | names.append(f"{first_letter}{second_letter}{i:03d}") |
| 84 | |
| 85 | # Shuffle the names randomly |
| 86 | random.shuffle(names) |
| 87 | cls._name_pool = names |
| 88 | |
| 89 | # Handled Edge Cases: Initialize the name pool if it's empty, handle the case where all names are used |
| 90 | # Handled Edge Cases: Handle resetting a robot that has a name, handle the case where all names are used |
| 91 | # Handled Edge Cases: Handle name assignment when name pool is exhausted |
| 92 | # Handled Edge Cases: Handle name pool generation when it's already populated |