yahoo

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 provided, converting it to years
6 for each planet according to their orbital periods relative to Earth years.
7
8 Attributes:
9 seconds (int): The age in seconds.
10 earth_year_in_seconds (float): The number of seconds in one Earth year (31,557,600).
11 """
12
13 # Earth year in seconds
14 EARTH_YEAR_IN_SECONDS = 31557600
15
16 # Orbital periods relative to Earth years
17 ORBITAL_PERIODS = {
18 'mercury': 0.2408467,
19 'venus': 0.61519726,
20 'earth': 1.0,
21 'mars': 1.8808158,
22 'jupiter': 11.862615,
23 'saturn': 29.447498,
24 'uranus': 84.016846,
25 'neptune': 164.79132
26 }
27
228 def __init__(self, seconds: int) -> None:
3 pass
29 """
30 Initialize the SpaceAge class with the given number of seconds.
31
32 Args:
33 seconds (int): The age in seconds.
34
35 # Edge Case: Negative seconds input
36 # The class accepts negative seconds but will produce negative ages, which is mathematically correct.
37 # Edge Case: Zero seconds input
38 # Zero seconds will correctly result in zero age on all planets.
39 """
40 self.seconds = seconds
41
42 def _calculate_age_on_planet(self, planet: str) -> float:
43 """
44 Calculate the age on a specific planet.
45
46 Args:
47 planet (str): The name of the planet (lowercase).
48
49 Returns:
50 float: The age on the specified planet, rounded to 2 decimal places.
51
52 # Edge Case: Division by zero
53 # This is prevented because Earth year in seconds is a constant and orbital periods are non-zero.
54 """
55 # Calculate Earth years
56 earth_years = self.seconds / self.EARTH_YEAR_IN_SECONDS
57
58 # Calculate age on the specified planet
59 planet_age = earth_years / self.ORBITAL_PERIODS[planet]
60
61 # Round to 2 decimal places
62 return round(planet_age, 2)
63
64 def on_mercury(self) -> float:
65 """
66 Calculate the age on Mercury.
67
68 Returns:
69 float: The age on Mercury, rounded to 2 decimal places.
70 """
71 return self._calculate_age_on_planet('mercury')
72 # Handled Edge Cases: Negative seconds, Zero seconds
73
74 def on_venus(self) -> float:
75 """
76 Calculate the age on Venus.
77
78 Returns:
79 float: The age on Venus, rounded to 2 decimal places.
80 """
81 return self._calculate_age_on_planet('venus')
82 # Handled Edge Cases: Negative seconds, Zero seconds
83
84 def on_earth(self) -> float:
85 """
86 Calculate the age on Earth.
87
88 Returns:
89 float: The age on Earth, rounded to 2 decimal places.
90 """
91 return self._calculate_age_on_planet('earth')
92 # Handled Edge Cases: Negative seconds, Zero seconds
93
94 def on_mars(self) -> float:
95 """
96 Calculate the age on Mars.
97
98 Returns:
99 float: The age on Mars, rounded to 2 decimal places.
100 """
101 return self._calculate_age_on_planet('mars')
102 # Handled Edge Cases: Negative seconds, Zero seconds
103
104 def on_jupiter(self) -> float:
105 """
106 Calculate the age on Jupiter.
107
108 Returns:
109 float: The age on Jupiter, rounded to 2 decimal places.
110 """
111 return self._calculate_age_on_planet('jupiter')
112 # Handled Edge Cases: Negative seconds, Zero seconds
113
114 def on_saturn(self) -> float:
115 """
116 Calculate the age on Saturn.
117
118 Returns:
119 float: The age on Saturn, rounded to 2 decimal places.
120 """
121 return self._calculate_age_on_planet('saturn')
122 # Handled Edge Cases: Negative seconds, Zero seconds
123
124 def on_uranus(self) -> float:
125 """
126 Calculate the age on Uranus.
127
128 Returns:
129 float: The age on Uranus, rounded to 2 decimal places.
130 """
131 return self._calculate_age_on_planet('uranus')
132 # Handled Edge Cases: Negative seconds, Zero seconds
133
134 def on_neptune(self) -> float:
135 """
136 Calculate the age on Neptune.
137
138 Returns:
139 float: The age on Neptune, rounded to 2 decimal places.
140 """
141 return self._calculate_age_on_planet('neptune')
142 # Handled Edge Cases: Negative seconds, Zero seconds
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.