Merry

Finished
1import random
2import string
3
14class Robot:
5 """
6 A class representing a robot with a randomly generated name.
7
8 The robot's name consists of two uppercase letters followed by three digits.
9 The name is generated when first accessed and can be reset to generate a new name.
10 """
11
12 # Class variable to keep track of all names that have been used
13 _used_names = set()
14
215 def __init__(self):
3 pass
4
16 """
17 Initialize a Robot instance with no name.
18 The name will be generated when first accessed.
19 """
20 self._name = None
21
522 @property
623 def name(self) -> str:
7 pass
8
24 """
25 Get the robot's name. If the robot doesn't have a name yet,
26 generate a new unique random name.
27
28 Returns:
29 str: The robot's name in the format of two uppercase letters followed by three digits.
30 """
31 # Edge Case: Robot has no name yet
32 if self._name is None:
33 self._generate_unique_name()
34 return self._name
35
936 def reset(self) -> None:
10 pass
37 """
38 Reset the robot to its factory settings, wiping its name.
39 The next time the name is accessed, a new random name will be generated.
40 """
41 # Edge Case: Robot has a name that needs to be removed from used names
42 if self._name is not None:
43 Robot._used_names.discard(self._name)
44 self._name = None
45
46 def _generate_unique_name(self) -> None:
47 """
48 Generate a unique random name for the robot.
49 The name consists of two uppercase letters followed by three digits.
50 Ensures that the generated name is unique among all robots ever created.
51 """
52 # Edge Case: All possible names are already used (theoretical limit)
53 # In practice, with 676,000 possible names (26*26*10*10*10), this is extremely unlikely
54 max_attempts = 1000 # Prevent infinite loop in case of issues
55 attempts = 0
56
57 while attempts < max_attempts:
58 # Generate two random uppercase letters
59 letters = ''.join(random.choices(string.ascii_uppercase, k=2))
60 # Generate three random digits
61 digits = ''.join(random.choices(string.digits, k=3))
62 # Combine to form the name
63 new_name = letters + digits
64
65 # Check if this name is unique
66 if new_name not in Robot._used_names:
67 Robot._used_names.add(new_name)
68 self._name = new_name
69 return
70
71 attempts += 1
72
73 # This should never happen in practice
74 raise RuntimeError("Unable to generate a unique name after maximum attempts")
75
76# Handled Edge Cases: Robot has no name yet, Robot has a name that needs to be removed from used names, All possible names are already used (theoretical limit)"
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.