perfect

Finished
1import random
2import string
3
14class Robot:
5 """A class representing a robot with a randomly generated name."""
6
7 # Class variable to keep track of all used names
8 _used_names = set()
9
210 def __init__(self):
3 pass
4
11 """Initialize a robot with no name."""
12 self._name = None
13
514 @property
615 def name(self) -> str:
7 pass
8
16 """Get the robot's name, generating one if it doesn't exist.
17
18 Returns:
19 str: The robot's name in format of two uppercase letters followed by three digits.
20 """
21 # Edge Case: Robot has no name yet, generate a new one
22 if self._name is None:
23 self._generate_name()
24 return self._name
25
926 def reset(self) -> None:
10 pass
27 """Reset the robot to factory settings, wiping its name.
28
29 This removes the current name from the used names set and sets the name to None.
30 """
31 # Edge Case: Robot has a name that needs to be removed from used names
32 if self._name is not None:
33 self._used_names.discard(self._name)
34 self._name = None
35
36 def _generate_name(self) -> None:
37 """Generate a unique random name for the robot.
38
39 The name format is two uppercase letters followed by three digits.
40 Ensures the generated name is unique across all robots.
41 """
42 # Edge Case: Handle potential name collision by retrying until unique name is found
43 while True:
44 # Generate two random uppercase letters
45 letters = ''.join(random.choices(string.ascii_uppercase, k=2))
46 # Generate three random digits
47 digits = ''.join(random.choices(string.digits, k=3))
48 # Combine to form the name
49 new_name = letters + digits
50
51 # Check if this name is already used
52 if new_name not in self._used_names:
53 self._name = new_name
54 self._used_names.add(new_name)
55 break
56
57 # Handled Edge Cases: Robot has no name yet, Robot has a name that needs to be removed from used names, Handle potential name collision by retrying until unique name is found
Test NameStatus
test_reset_name
Fail
test_different_robots_have_different_names
Pass
test_has_name
Pass
test_name_sticks
Pass

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