| 1 | 1 | class SpaceAge: |
| 2 | """ |
| 3 | A class to calculate the age of a person on different planets in our Solar System |
| 4 | based on their age in seconds. |
| 5 | |
| 6 | The class takes an age in seconds and provides methods to calculate the age |
| 7 | on various planets, accounting for their orbital periods relative to Earth. |
| 8 | |
| 9 | Args: |
| 10 | seconds (int): The age in seconds |
| 11 | """ |
| 12 | |
| 13 | # Earth year in seconds |
| 14 | EARTH_YEAR_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 | |
| 2 | 28 | def __init__(self, seconds: int) -> None: |
| 3 | | pass |
| 29 | """ |
| 30 | Initialize the SpaceAge object with an age in seconds. |
| 31 | |
| 32 | Args: |
| 33 | seconds (int): The age in seconds |
| 34 | """ |
| 35 | # Edge Case: Negative seconds input |
| 36 | if seconds < 0: |
| 37 | raise ValueError("Seconds cannot be negative") |
| 38 | |
| 39 | self.seconds = seconds |
| 40 | self.earth_years = seconds / self.EARTH_YEAR_SECONDS |
| 41 | |
| 42 | def on_mercury(self) -> float: |
| 43 | """ |
| 44 | Calculate the age on Mercury. |
| 45 | |
| 46 | Returns: |
| 47 | float: Age on Mercury rounded to 2 decimal places |
| 48 | """ |
| 49 | age_on_planet = self.earth_years / self.ORBITAL_PERIODS['mercury'] |
| 50 | return round(age_on_planet, 2) |
| 51 | |
| 52 | def on_venus(self) -> float: |
| 53 | """ |
| 54 | Calculate the age on Venus. |
| 55 | |
| 56 | Returns: |
| 57 | float: Age on Venus rounded to 2 decimal places |
| 58 | """ |
| 59 | age_on_planet = self.earth_years / self.ORBITAL_PERIODS['venus'] |
| 60 | return round(age_on_planet, 2) |
| 61 | |
| 62 | def on_earth(self) -> float: |
| 63 | """ |
| 64 | Calculate the age on Earth. |
| 65 | |
| 66 | Returns: |
| 67 | float: Age on Earth rounded to 2 decimal places |
| 68 | """ |
| 69 | # Edge Case: Direct calculation for Earth (period is 1.0) |
| 70 | return round(self.earth_years, 2) |
| 71 | |
| 72 | def on_mars(self) -> float: |
| 73 | """ |
| 74 | Calculate the age on Mars. |
| 75 | |
| 76 | Returns: |
| 77 | float: Age on Mars rounded to 2 decimal places |
| 78 | """ |
| 79 | age_on_planet = self.earth_years / self.ORBITAL_PERIODS['mars'] |
| 80 | return round(age_on_planet, 2) |
| 81 | |
| 82 | def on_jupiter(self) -> float: |
| 83 | """ |
| 84 | Calculate the age on Jupiter. |
| 85 | |
| 86 | Returns: |
| 87 | float: Age on Jupiter rounded to 2 decimal places |
| 88 | """ |
| 89 | age_on_planet = self.earth_years / self.ORBITAL_PERIODS['jupiter'] |
| 90 | return round(age_on_planet, 2) |
| 91 | |
| 92 | def on_saturn(self) -> float: |
| 93 | """ |
| 94 | Calculate the age on Saturn. |
| 95 | |
| 96 | Returns: |
| 97 | float: Age on Saturn rounded to 2 decimal places |
| 98 | """ |
| 99 | age_on_planet = self.earth_years / self.ORBITAL_PERIODS['saturn'] |
| 100 | return round(age_on_planet, 2) |
| 101 | |
| 102 | def on_uranus(self) -> float: |
| 103 | """ |
| 104 | Calculate the age on Uranus. |
| 105 | |
| 106 | Returns: |
| 107 | float: Age on Uranus rounded to 2 decimal places |
| 108 | """ |
| 109 | age_on_planet = self.earth_years / self.ORBITAL_PERIODS['uranus'] |
| 110 | return round(age_on_planet, 2) |
| 111 | |
| 112 | def on_neptune(self) -> float: |
| 113 | """ |
| 114 | Calculate the age on Neptune. |
| 115 | |
| 116 | Returns: |
| 117 | float: Age on Neptune rounded to 2 decimal places |
| 118 | """ |
| 119 | age_on_planet = self.earth_years / self.ORBITAL_PERIODS['neptune'] |
| 120 | return round(age_on_planet, 2) |
| 121 | |
| 122 | # Handled Edge Cases: Negative seconds input, Direct calculation for Earth (period is 1.0) |