A.2.1

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 assigned from a pre-generated pool.
10 """
11
12 # Class-level name pool and used names tracking
13 _name_pool = []
14 _used_names = set()
15
216 def __init__(self):
3 pass
4
17 """
18 Initialize a Robot instance.
19
20 The robot starts with no name assigned. A name will be generated when first accessed.
21 """
22 self._name = None
23 # Generate name pool if not already done
24 if not Robot._name_pool:
25 self._generate_name_pool()
26
527 @property
628 def name(self) -> str:
7 pass
8
29 """
30 Get the robot's name.
31
32 If the robot doesn't have a name yet, assign one randomly from the available pool.
33
34 Returns:
35 str: The robot's name in format AA000.
36 """
37 if self._name is None:
38 # Edge Case: No names available in pool
39 if not Robot._name_pool:
40 raise RuntimeError("No more names available for robots")
41
42 # Assign a name from the pool
43 self._name = Robot._name_pool.pop()
44 Robot._used_names.add(self._name)
45 return self._name
46
947 def reset(self) -> None:
10 pass
48 """
49 Reset the robot to factory settings.
50
51 This wipes the robot's current name and makes it available for reuse.
52 """
53 # Edge Case: Robot has no name to reset
54 if self._name is not None:
55 # Return the name to the pool for potential reuse
56 Robot._used_names.remove(self._name)
57 Robot._name_pool.append(self._name)
58 # Shuffle to maintain randomness
59 random.shuffle(Robot._name_pool)
60 self._name = None
61
62 @classmethod
63 def _generate_name_pool(cls):
64 """
65 Generate all possible valid robot names and shuffle them randomly.
66
67 This method is called once to create the pool of all possible names.
68 """
69 # Edge Case: Prevent multiple generations
70 if cls._name_pool:
71 return
72
73 letters = string.ascii_uppercase
74 digits = string.digits
75
76 # Generate all possible combinations: AA000 to ZZ999
77 for first_letter in letters:
78 for second_letter in letters:
79 for digit1 in digits:
80 for digit2 in digits:
81 for digit3 in digits:
82 name = f"{first_letter}{second_letter}{digit1}{digit2}{digit3}"
83 cls._name_pool.append(name)
84
85 # Shuffle the pool to ensure randomness
86 random.shuffle(cls._name_pool)
87
88 # Handled Edge Cases: No names available in pool, Robot has no name to reset, Prevent multiple generations
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.