sn62

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 Each robot has a unique name, and the name can be reset to a new random one.
10 """
11
12 # Class-level name pool to ensure uniqueness across all robot instances
13 _name_pool = []
14 _used_names = set()
15
216 def __init__(self):
3 pass
4
17 """
18 Initialize a new Robot instance with no name.
19 The actual name is generated when first accessed.
20 """
21 self._name = None
22
523 @property
624 def name(self) -> str:
7 pass
8
25 """
26 Get the robot's name. If the robot doesn't have a name yet, generate one.
27
28 Returns:
29 str: The robot's name in format AA000
30 """
31 # Edge Case: Robot has no name yet - generate one
32 if self._name is None:
33 # Edge Case: Name pool is empty or exhausted - this should not happen with the pre-generated approach
34 if not Robot._name_pool:
35 Robot._generate_name_pool()
36
37 # Edge Case: All possible names are used - this should not happen in normal operation
38 if not Robot._name_pool:
39 raise RuntimeError("All possible robot names have been used.")
40
41 self._name = Robot._name_pool.pop()
42 Robot._used_names.add(self._name)
43
44 return self._name
45
946 def reset(self) -> None:
10 pass
47 """
48 Reset the robot to factory settings, wiping its name.
49 The next time the name is accessed, a new random name will be generated.
50 """
51 # Edge Case: Robot has a name that needs to be returned to the pool
52 if self._name is not None:
53 # Remove from used names but don't put back in pool to maintain uniqueness
54 Robot._used_names.discard(self._name)
55 self._name = None
56
57 @classmethod
58 def _generate_name_pool(cls) -> None:
59 """
60 Generate all possible valid robot names, shuffle them randomly,
61 and store them in the name pool.
62 """
63 # Edge Case: Prevent regenerating if already generated
64 if cls._name_pool:
65 return
66
67 names = []
68 # Generate all possible combinations of 2 letters + 3 digits
69 for first_letter in string.ascii_uppercase:
70 for second_letter in string.ascii_uppercase:
71 for number in range(1000):
72 name = f"{first_letter}{second_letter}{number:03d}"
73 # Edge Case: Ensure we don't generate a name that's already used
74 if name not in cls._used_names:
75 names.append(name)
76
77 # Shuffle the names randomly
78 random.shuffle(names)
79 cls._name_pool = names
80
81# Handled Edge Cases: Robot has no name yet, Name pool is empty or exhausted, All possible names are used, Robot has a name that needs to be returned to the pool, Prevent regenerating if already generated
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.