| 1 | 1 | class SpaceAge: |
| 2 | # Earth year in seconds |
| 3 | EARTH_YEAR_IN_SECONDS = 31557600 |
| 4 | |
| 5 | # Orbital periods in Earth years |
| 6 | ORBITAL_PERIODS = { |
| 7 | 'mercury': 0.2408467, |
| 8 | 'venus': 0.61519726, |
| 9 | 'earth': 1.0, |
| 10 | 'mars': 1.8808158, |
| 11 | 'jupiter': 11.862615, |
| 12 | 'saturn': 29.447498, |
| 13 | 'uranus': 84.016846, |
| 14 | 'neptune': 164.79132 |
| 15 | } |
| 16 | |
| 2 | 17 | def __init__(self, seconds: int) -> None: |
| 3 | | pass |
| 18 | # Edge Case: Negative seconds input |
| 19 | if seconds < 0: |
| 20 | raise ValueError("Seconds cannot be negative") |
| 21 | self.seconds = seconds |
| 22 | self.earth_years = seconds / self.EARTH_YEAR_IN_SECONDS |
| 23 | |
| 24 | def _calculate_planet_age(self, orbital_period: float) -> float: |
| 25 | """Helper method to calculate age on a planet with proper rounding handling.""" |
| 26 | age = self.earth_years / orbital_period |
| 27 | rounded_age = round(age, 2) |
| 28 | # If rounding to 2 decimal places results in 0.0, return full precision |
| 29 | if rounded_age == 0.0: |
| 30 | return age |
| 31 | return rounded_age |
| 32 | |
| 33 | def on_mercury(self) -> float: |
| 34 | return self._calculate_planet_age(self.ORBITAL_PERIODS['mercury']) |
| 35 | |
| 36 | def on_venus(self) -> float: |
| 37 | return self._calculate_planet_age(self.ORBITAL_PERIODS['venus']) |
| 38 | |
| 39 | def on_earth(self) -> float: |
| 40 | rounded_age = round(self.earth_years, 2) |
| 41 | # If rounding to 2 decimal places results in 0.0, return full precision |
| 42 | if rounded_age == 0.0: |
| 43 | return self.earth_years |
| 44 | return rounded_age |
| 45 | |
| 46 | def on_mars(self) -> float: |
| 47 | return self._calculate_planet_age(self.ORBITAL_PERIODS['mars']) |
| 48 | |
| 49 | def on_jupiter(self) -> float: |
| 50 | return self._calculate_planet_age(self.ORBITAL_PERIODS['jupiter']) |
| 51 | |
| 52 | def on_saturn(self) -> float: |
| 53 | return self._calculate_planet_age(self.ORBITAL_PERIODS['saturn']) |
| 54 | |
| 55 | def on_uranus(self) -> float: |
| 56 | return self._calculate_planet_age(self.ORBITAL_PERIODS['uranus']) |
| 57 | |
| 58 | def on_neptune(self) -> float: |
| 59 | return self._calculate_planet_age(self.ORBITAL_PERIODS['neptune']) |
| 60 | # Handled Edge Cases: Negative seconds input |