| 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 provides methods for each planet that return the age on that planet |
| 7 | in Earth years, rounded to two decimal places. |
| 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 in 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 the given age in seconds. |
| 31 | |
| 32 | Args: |
| 33 | seconds (int): The age in seconds |
| 34 | """ |
| 35 | # Edge Case: Handle negative seconds by taking absolute value |
| 36 | self.seconds = abs(seconds) |
| 37 | |
| 38 | # Calculate Earth age in years |
| 39 | self.earth_years = self.seconds / self.EARTH_YEAR_SECONDS |
| 40 | |
| 41 | def on_mercury(self) -> float: |
| 42 | """ |
| 43 | Calculate the age on Mercury. |
| 44 | |
| 45 | Returns: |
| 46 | float: Age on Mercury in Earth years, rounded to 2 decimal places |
| 47 | """ |
| 48 | age = self.earth_years / self.ORBITAL_PERIODS['mercury'] |
| 49 | return round(age, 2) |
| 50 | |
| 51 | def on_venus(self) -> float: |
| 52 | """ |
| 53 | Calculate the age on Venus. |
| 54 | |
| 55 | Returns: |
| 56 | float: Age on Venus in Earth years, rounded to 2 decimal places |
| 57 | """ |
| 58 | age = self.earth_years / self.ORBITAL_PERIODS['venus'] |
| 59 | return round(age, 2) |
| 60 | |
| 61 | def on_earth(self) -> float: |
| 62 | """ |
| 63 | Calculate the age on Earth. |
| 64 | |
| 65 | Returns: |
| 66 | float: Age on Earth in Earth years, rounded to 2 decimal places |
| 67 | """ |
| 68 | # Edge Case: Direct return since Earth's orbital period is 1.0 |
| 69 | return round(self.earth_years, 2) |
| 70 | |
| 71 | def on_mars(self) -> float: |
| 72 | """ |
| 73 | Calculate the age on Mars. |
| 74 | |
| 75 | Returns: |
| 76 | float: Age on Mars in Earth years, rounded to 2 decimal places |
| 77 | """ |
| 78 | age = self.earth_years / self.ORBITAL_PERIODS['mars'] |
| 79 | return round(age, 2) |
| 80 | |
| 81 | def on_jupiter(self) -> float: |
| 82 | """ |
| 83 | Calculate the age on Jupiter. |
| 84 | |
| 85 | Returns: |
| 86 | float: Age on Jupiter in Earth years, rounded to 2 decimal places |
| 87 | """ |
| 88 | age = self.earth_years / self.ORBITAL_PERIODS['jupiter'] |
| 89 | return round(age, 2) |
| 90 | |
| 91 | def on_saturn(self) -> float: |
| 92 | """ |
| 93 | Calculate the age on Saturn. |
| 94 | |
| 95 | Returns: |
| 96 | float: Age on Saturn in Earth years, rounded to 2 decimal places |
| 97 | """ |
| 98 | age = self.earth_years / self.ORBITAL_PERIODS['saturn'] |
| 99 | return round(age, 2) |
| 100 | |
| 101 | def on_uranus(self) -> float: |
| 102 | """ |
| 103 | Calculate the age on Uranus. |
| 104 | |
| 105 | Returns: |
| 106 | float: Age on Uranus in Earth years, rounded to 2 decimal places |
| 107 | """ |
| 108 | age = self.earth_years / self.ORBITAL_PERIODS['uranus'] |
| 109 | return round(age, 2) |
| 110 | |
| 111 | def on_neptune(self) -> float: |
| 112 | """ |
| 113 | Calculate the age on Neptune. |
| 114 | |
| 115 | Returns: |
| 116 | float: Age on Neptune in Earth years, rounded to 2 decimal places |
| 117 | """ |
| 118 | age = self.earth_years / self.ORBITAL_PERIODS['neptune'] |
| 119 | return round(age, 2) |
| 120 | |
| 121 | # Handled Edge Cases: Negative seconds input |