xas

Finished
11class SpaceAge:
2 """
3 A class to calculate the age of a person on different planets in our Solar System.
4
5 The age is given in seconds and converted to years based on the orbital periods
6 of the respective planets relative to Earth years.
7
8 Methods:
9 on_mercury(): Returns age on Mercury in Earth years.
10 on_venus(): Returns age on Venus in Earth years.
11 on_earth(): Returns age on Earth in Earth years.
12 on_mars(): Returns age on Mars in Earth years.
13 on_jupiter(): Returns age on Jupiter in Earth years.
14 on_saturn(): Returns age on Saturn in Earth years.
15 on_uranus(): Returns age on Uranus in Earth years.
16 on_neptune(): Returns age on Neptune in Earth years.
17 """
18
19 # Earth year in seconds
20 EARTH_YEAR_SECONDS = 31557600
21
22 # Orbital periods relative to Earth years
23 ORBITAL_PERIODS = {
24 'mercury': 0.2408467,
25 'venus': 0.61519726,
26 'earth': 1.0,
27 'mars': 1.8808158,
28 'jupiter': 11.862615,
29 'saturn': 29.447498,
30 'uranus': 84.016846,
31 'neptune': 164.79132
32 }
33
234 def __init__(self, seconds: int) -> None:
3 pass
35 """
36 Initialize the SpaceAge object with the given age in seconds.
37
38 Args:
39 seconds (int): The age in seconds. Must be non-negative.
40
41 Raises:
42 ValueError: If seconds is negative.
43 """
44 # Edge Case: Negative seconds input
45 if seconds < 0:
46 raise ValueError("Seconds cannot be negative")
47
48 self.seconds = seconds
49
50 def _calculate_age_on_planet(self, planet: str) -> float:
51 """
52 Helper method to calculate age on a specific planet.
53
54 Args:
55 planet (str): The name of the planet.
56
57 Returns:
58 float: The age on the specified planet, rounded to 2 decimal places.
59 """
60 # Calculate the age in Earth years
61 earth_years = self.seconds / self.EARTH_YEAR_SECONDS
62
63 # Convert to the planet's years
64 planet_years = earth_years / self.ORBITAL_PERIODS[planet]
65
66 # Round to 2 decimal places
67 return round(planet_years, 2)
68
69 def on_mercury(self) -> float:
70 """
71 Calculate the age on Mercury.
72
73 Returns:
74 float: The age on Mercury in Earth years, rounded to 2 decimal places.
75 """
76 return self._calculate_age_on_planet('mercury')
77
78 def on_venus(self) -> float:
79 """
80 Calculate the age on Venus.
81
82 Returns:
83 float: The age on Venus in Earth years, rounded to 2 decimal places.
84 """
85 return self._calculate_age_on_planet('venus')
86
87 def on_earth(self) -> float:
88 """
89 Calculate the age on Earth.
90
91 Returns:
92 float: The age on Earth in Earth years, rounded to 2 decimal places.
93 """
94 return self._calculate_age_on_planet('earth')
95
96 def on_mars(self) -> float:
97 """
98 Calculate the age on Mars.
99
100 Returns:
101 float: The age on Mars in Earth years, rounded to 2 decimal places.
102 """
103 return self._calculate_age_on_planet('mars')
104
105 def on_jupiter(self) -> float:
106 """
107 Calculate the age on Jupiter.
108
109 Returns:
110 float: The age on Jupiter in Earth years, rounded to 2 decimal places.
111 """
112 return self._calculate_age_on_planet('jupiter')
113
114 def on_saturn(self) -> float:
115 """
116 Calculate the age on Saturn.
117
118 Returns:
119 float: The age on Saturn in Earth years, rounded to 2 decimal places.
120 """
121 return self._calculate_age_on_planet('saturn')
122
123 def on_uranus(self) -> float:
124 """
125 Calculate the age on Uranus.
126
127 Returns:
128 float: The age on Uranus in Earth years, rounded to 2 decimal places.
129 """
130 return self._calculate_age_on_planet('uranus')
131
132 def on_neptune(self) -> float:
133 """
134 Calculate the age on Neptune.
135
136 Returns:
137 float: The age on Neptune in Earth years, rounded to 2 decimal places.
138 """
139 return self._calculate_age_on_planet('neptune')
140
141# Handled Edge Cases: Negative seconds input
Test NameStatus
test_age_on_earth
Pass
test_age_on_jupiter
Pass
test_age_on_mars
Pass
test_age_on_mercury
Pass
test_age_on_neptune
Pass
test_age_on_saturn
Pass
test_age_on_uranus
Pass
test_age_on_venus
Pass

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