sn62

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 calculated based on the number of seconds passed since birth,
6 and converted to the equivalent age on various planets considering their
7 orbital periods relative to Earth years.
8
9 Attributes:
10 seconds (int): The age in seconds.
11 earth_year_in_seconds (float): Number of seconds in one Earth year (31,557,600).
12 """
13
14 # Define the number of seconds in one Earth year
15 EARTH_YEAR_IN_SECONDS = 31557600
16
17 # Define the orbital periods for each planet in Earth years
18 ORBITAL_PERIODS = {
19 'mercury': 0.2408467,
20 'venus': 0.61519726,
21 'earth': 1.0,
22 'mars': 1.8808158,
23 'jupiter': 11.862615,
24 'saturn': 29.447498,
25 'uranus': 84.016846,
26 'neptune': 164.79132
27 }
28
229 def __init__(self, seconds: int) -> None:
3 pass
30 """
31 Initialize the SpaceAge object with the given number of seconds.
32
33 Args:
34 seconds (int): The age in seconds.
35 """
36 # Edge Case: Handle negative seconds by taking absolute value
37 self.seconds = abs(seconds)
38
39 def _calculate_age_on_planet(self, planet: str) -> float:
40 """
41 Calculate the age on a specific planet.
42
43 Args:
44 planet (str): The name of the planet (lowercase).
45
46 Returns:
47 float: The age on the specified planet, rounded to 2 decimal places.
48 """
49 # Edge Case: Handle division by zero if somehow EARTH_YEAR_IN_SECONDS was 0
50 if self.EARTH_YEAR_IN_SECONDS == 0:
51 return 0.0
52
53 # Edge Case: Handle if planet is not in our defined orbital periods
54 if planet not in self.ORBITAL_PERIODS:
55 raise ValueError(f"Planet '{planet}' is not supported.")
56
57 # Calculate Earth years from seconds
58 earth_years = self.seconds / self.EARTH_YEAR_IN_SECONDS
59
60 # Calculate age on the specified planet
61 planet_age = earth_years / self.ORBITAL_PERIODS[planet]
62
63 # Round to 2 decimal places
64 return round(planet_age, 2)
65
66 def on_mercury(self) -> float:
67 """
68 Calculate the age on Mercury.
69
70 Returns:
71 float: The age on Mercury, rounded to 2 decimal places.
72 """
73 return self._calculate_age_on_planet('mercury')
74 # Handled Edge Cases: Negative seconds, unsupported planet, division by zero
75
76 def on_venus(self) -> float:
77 """
78 Calculate the age on Venus.
79
80 Returns:
81 float: The age on Venus, rounded to 2 decimal places.
82 """
83 return self._calculate_age_on_planet('venus')
84 # Handled Edge Cases: Negative seconds, unsupported planet, division by zero
85
86 def on_earth(self) -> float:
87 """
88 Calculate the age on Earth.
89
90 Returns:
91 float: The age on Earth, rounded to 2 decimal places.
92 """
93 return self._calculate_age_on_planet('earth')
94 # Handled Edge Cases: Negative seconds, unsupported planet, division by zero
95
96 def on_mars(self) -> float:
97 """
98 Calculate the age on Mars.
99
100 Returns:
101 float: The age on Mars, rounded to 2 decimal places.
102 """
103 return self._calculate_age_on_planet('mars')
104 # Handled Edge Cases: Negative seconds, unsupported planet, division by zero
105
106 def on_jupiter(self) -> float:
107 """
108 Calculate the age on Jupiter.
109
110 Returns:
111 float: The age on Jupiter, rounded to 2 decimal places.
112 """
113 return self._calculate_age_on_planet('jupiter')
114 # Handled Edge Cases: Negative seconds, unsupported planet, division by zero
115
116 def on_saturn(self) -> float:
117 """
118 Calculate the age on Saturn.
119
120 Returns:
121 float: The age on Saturn, rounded to 2 decimal places.
122 """
123 return self._calculate_age_on_planet('saturn')
124 # Handled Edge Cases: Negative seconds, unsupported planet, division by zero
125
126 def on_uranus(self) -> float:
127 """
128 Calculate the age on Uranus.
129
130 Returns:
131 float: The age on Uranus, rounded to 2 decimal places.
132 """
133 return self._calculate_age_on_planet('uranus')
134 # Handled Edge Cases: Negative seconds, unsupported planet, division by zero
135
136 def on_neptune(self) -> float:
137 """
138 Calculate the age on Neptune.
139
140 Returns:
141 float: The age on Neptune, rounded to 2 decimal places.
142 """
143 return self._calculate_age_on_planet('neptune')
144 # Handled Edge Cases: Negative seconds, unsupported planet, division by zero
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.