Finished
1import random
2import string
3
14class 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 followed by three digits.
9 Names are unique and randomly selected from a pre-generated pool.
10 """
11
12 # Class-level set to track all used names across all robot instances
13 _used_names = set()
14
15 # Pre-generate all possible valid names
16 _name_pool = [
17 f"{letter1}{letter2}{digit1}{digit2}{digit3}"
18 for letter1 in string.ascii_uppercase
19 for letter2 in string.ascii_uppercase
20 for digit1 in string.digits
21 for digit2 in string.digits
22 for digit3 in string.digits
23 ]
24
25 # Shuffle the name pool to ensure randomness
26 random.shuffle(_name_pool)
27
28 # Class-level index to track the next available name
29 _next_index = 0
30
231 def __init__(self):
3 pass
4
32 """
33 Initialize a new Robot instance with no name.
34 The actual name is assigned on first access.
35 """
36 self._name = None
37
538 @property
639 def name(self) -> str:
7 pass
8
40 """
41 Get the robot's name. If the robot doesn't have a name yet,
42 assign one randomly from the available pool.
43
44 Returns:
45 str: The robot's name in format of two uppercase letters followed by three digits.
46
47 Raises:
48 RuntimeError: If all possible names have been used.
49 """
50 # Edge Case: Robot already has a name, return it
51 if self._name is not None:
52 return self._name
53
54 # Edge Case: All possible names have been used
55 if Robot._next_index >= len(Robot._name_pool):
56 raise RuntimeError("No more unique names available")
57
58 # Assign a new name from the pool
59 new_name = Robot._name_pool[Robot._next_index]
60 Robot._next_index += 1
61 Robot._used_names.add(new_name)
62 self._name = new_name
63 return self._name
64
965 def reset(self) -> None:
10 pass
66 """
67 Reset the robot to factory settings, wiping its name.
68 The name is returned to the pool and can be reassigned to other robots.
69 """
70 # Edge Case: Robot has no name to reset
71 if self._name is None:
72 return
73
74 # Return the name to the pool of available names
75 Robot._used_names.discard(self._name)
76
77 # Note: We don't actually return the name to the pool to be reused
78 # as the problem states "Every robot ever created should have a unique name."
79 # This means names should never be reused, even after reset.
80
81 # Clear the robot's name
82 self._name = None
83
84 # Handled Edge Cases: Robot already has a name, all possible names have been used, robot has no name to reset
Test NameStatus
test_different_robots_have_different_names
Pass
test_has_name
Pass
test_name_sticks
Pass
test_reset_name
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.