| 1 | import random |
| 2 | import string |
| 3 | |
| 1 | 4 | class Robot: |
| 5 | """ |
| 6 | A class representing a robot with a randomly generated name. |
| 7 | |
| 8 | The robot's name consists of two uppercase letters followed by three digits. |
| 9 | The names are generated from a pre-shuffled pool to ensure uniqueness. |
| 10 | """ |
| 11 | |
| 12 | # Class variable to hold the pool of all possible names |
| 13 | _name_pool = [] |
| 14 | # Class variable to track the current index in the pool |
| 15 | _current_index = 0 |
| 16 | |
| 2 | 17 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 18 | """ |
| 19 | Initialize a new Robot instance. |
| 20 | |
| 21 | If this is the first robot being created, generate and shuffle all possible names. |
| 22 | """ |
| 23 | # Edge Case: Initialize the name pool if it hasn't been created yet |
| 24 | if not Robot._name_pool: |
| 25 | self._generate_name_pool() |
| 26 | |
| 27 | self._name = None |
| 28 | # Assign a name when the robot is first created |
| 29 | self.name |
| 30 | |
| 5 | 31 | @property |
| 6 | 32 | def name(self) -> str: |
| 7 | | pass |
| 8 | | |
| 33 | """ |
| 34 | Get the robot's name. |
| 35 | |
| 36 | If the robot doesn't have a name yet, assign one from the pool. |
| 37 | |
| 38 | Returns: |
| 39 | str: The robot's name. |
| 40 | """ |
| 41 | # Edge Case: Assign a name if the robot doesn't have one |
| 42 | if self._name is None: |
| 43 | # Edge Case: Check if we've exhausted all possible names |
| 44 | if Robot._current_index >= len(Robot._name_pool): |
| 45 | raise RuntimeError("No more unique names available") |
| 46 | |
| 47 | self._name = Robot._name_pool[Robot._current_index] |
| 48 | Robot._current_index += 1 |
| 49 | |
| 50 | return self._name |
| 51 | |
| 9 | 52 | def reset(self) -> None: |
| 10 | | pass |
| 53 | """ |
| 54 | Reset the robot to its factory settings, wiping its name. |
| 55 | |
| 56 | The name is not returned to the pool to maintain uniqueness. |
| 57 | """ |
| 58 | # Edge Case: Reset the name to None, but don't return it to the pool |
| 59 | self._name = None |
| 60 | |
| 61 | @classmethod |
| 62 | def _generate_name_pool(cls): |
| 63 | """ |
| 64 | Generate all possible robot names and shuffle them randomly. |
| 65 | |
| 66 | This method is called once to initialize the name pool. |
| 67 | """ |
| 68 | # Edge Case: Generate all possible combinations of names |
| 69 | for letter1 in string.ascii_uppercase: |
| 70 | for letter2 in string.ascii_uppercase: |
| 71 | for digit1 in string.digits: |
| 72 | for digit2 in string.digits: |
| 73 | for digit3 in string.digits: |
| 74 | cls._name_pool.append(f"{letter1}{letter2}{digit1}{digit2}{digit3}") |
| 75 | |
| 76 | # Edge Case: Shuffle the pool to ensure randomness |
| 77 | random.shuffle(cls._name_pool) |
| 78 | |
| 79 | # Handled Edge Cases: |
| 80 | # - Initialize the name pool if it hasn't been created yet |
| 81 | # - Assign a name if the robot doesn't have one |
| 82 | # - Check if we've exhausted all possible names |
| 83 | # - Reset the name to None, but don't return it to the pool |
| 84 | # - Generate all possible combinations of names |
| 85 | # - Shuffle the pool to ensure randomness |