| 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). All names are unique. |
| 10 | """ |
| 11 | |
| 12 | # Class variable to store all possible names |
| 13 | _name_pool = None |
| 14 | |
| 2 | 15 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 16 | """ |
| 17 | Initialize a new Robot instance with no name. |
| 18 | """ |
| 19 | # Edge Case: Initialize the name pool if it hasn't been created yet |
| 20 | if Robot._name_pool is None: |
| 21 | Robot._generate_name_pool() |
| 22 | |
| 23 | # Edge Case: No names left in the pool |
| 24 | if not Robot._name_pool: |
| 25 | raise RuntimeError("No more unique names available") |
| 26 | |
| 27 | self._name = None |
| 28 | self.reset() |
| 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 in format AA000 |
| 37 | """ |
| 38 | return self._name |
| 39 | |
| 9 | 40 | def reset(self) -> None: |
| 10 | | pass |
| 41 | """ |
| 42 | Reset the robot to factory settings, assigning a new random name. |
| 43 | |
| 44 | This removes the current name from the pool of available names |
| 45 | and assigns a new one. |
| 46 | """ |
| 47 | # Edge Case: No names left in the pool |
| 48 | if not Robot._name_pool: |
| 49 | raise RuntimeError("No more unique names available") |
| 50 | |
| 51 | # If this robot had a previous name, return it to the pool |
| 52 | if self._name is not None: |
| 53 | Robot._name_pool.add(self._name) |
| 54 | |
| 55 | # Select a new name from the pool |
| 56 | new_name = random.choice(list(Robot._name_pool)) |
| 57 | Robot._name_pool.remove(new_name) |
| 58 | self._name = new_name |
| 59 | |
| 60 | @classmethod |
| 61 | def _generate_name_pool(cls): |
| 62 | """ |
| 63 | Generate all possible valid robot names and store them in a set. |
| 64 | |
| 65 | This creates 26*26*10*10*10 = 676,000 unique names in the format AA000. |
| 66 | """ |
| 67 | # Edge Case: Pool already exists |
| 68 | if cls._name_pool is not None: |
| 69 | return |
| 70 | |
| 71 | cls._name_pool = set() |
| 72 | for first_letter in string.ascii_uppercase: |
| 73 | for second_letter in string.ascii_uppercase: |
| 74 | for digit1 in string.digits: |
| 75 | for digit2 in string.digits: |
| 76 | for digit3 in string.digits: |
| 77 | name = f"{first_letter}{second_letter}{digit1}{digit2}{digit3}" |
| 78 | cls._name_pool.add(name) |
| 79 | |
| 80 | # Handled Edge Cases: Initialize the name pool if it hasn't been created yet, No names left in the pool |
| 81 | # Handled Edge Cases: No names left in the pool, Return previous name to pool when resetting |
| 82 | # Handled Edge Cases: Pool already exists |