| 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 | # Calculate Earth age in years |
| 38 | self.earth_years = self.seconds / self.EARTH_YEAR_SECONDS |
| 39 | |
| 40 | def on_mercury(self) -> float: |
| 41 | """ |
| 42 | Calculate the age on Mercury. |
| 43 | |
| 44 | Returns: |
| 45 | float: Age on Mercury in Earth years, rounded to two decimal places |
| 46 | """ |
| 47 | age_on_mercury = self.earth_years / self.ORBITAL_PERIODS['mercury'] |
| 48 | return round(age_on_mercury, 2) |
| 49 | |
| 50 | def on_venus(self) -> float: |
| 51 | """ |
| 52 | Calculate the age on Venus. |
| 53 | |
| 54 | Returns: |
| 55 | float: Age on Venus in Earth years, rounded to two decimal places |
| 56 | """ |
| 57 | age_on_venus = self.earth_years / self.ORBITAL_PERIODS['venus'] |
| 58 | return round(age_on_venus, 2) |
| 59 | |
| 60 | def on_earth(self) -> float: |
| 61 | """ |
| 62 | Calculate the age on Earth. |
| 63 | |
| 64 | Returns: |
| 65 | float: Age on Earth in Earth years, rounded to two decimal places |
| 66 | """ |
| 67 | # Edge Case: Directly return the earth_years value rounded to 2 decimal places |
| 68 | return round(self.earth_years, 2) |
| 69 | |
| 70 | def on_mars(self) -> float: |
| 71 | """ |
| 72 | Calculate the age on Mars. |
| 73 | |
| 74 | Returns: |
| 75 | float: Age on Mars in Earth years, rounded to two decimal places |
| 76 | """ |
| 77 | age_on_mars = self.earth_years / self.ORBITAL_PERIODS['mars'] |
| 78 | return round(age_on_mars, 2) |
| 79 | |
| 80 | def on_jupiter(self) -> float: |
| 81 | """ |
| 82 | Calculate the age on Jupiter. |
| 83 | |
| 84 | Returns: |
| 85 | float: Age on Jupiter in Earth years, rounded to two decimal places |
| 86 | """ |
| 87 | age_on_jupiter = self.earth_years / self.ORBITAL_PERIODS['jupiter'] |
| 88 | return round(age_on_jupiter, 2) |
| 89 | |
| 90 | def on_saturn(self) -> float: |
| 91 | """ |
| 92 | Calculate the age on Saturn. |
| 93 | |
| 94 | Returns: |
| 95 | float: Age on Saturn in Earth years, rounded to two decimal places |
| 96 | """ |
| 97 | age_on_saturn = self.earth_years / self.ORBITAL_PERIODS['saturn'] |
| 98 | return round(age_on_saturn, 2) |
| 99 | |
| 100 | def on_uranus(self) -> float: |
| 101 | """ |
| 102 | Calculate the age on Uranus. |
| 103 | |
| 104 | Returns: |
| 105 | float: Age on Uranus in Earth years, rounded to two decimal places |
| 106 | """ |
| 107 | age_on_uranus = self.earth_years / self.ORBITAL_PERIODS['uranus'] |
| 108 | return round(age_on_uranus, 2) |
| 109 | |
| 110 | def on_neptune(self) -> float: |
| 111 | """ |
| 112 | Calculate the age on Neptune. |
| 113 | |
| 114 | Returns: |
| 115 | float: Age on Neptune in Earth years, rounded to two decimal places |
| 116 | """ |
| 117 | age_on_neptune = self.earth_years / self.ORBITAL_PERIODS['neptune'] |
| 118 | return round(age_on_neptune, 2) |
| 119 | |
| 120 | # Handled Edge Cases: Negative seconds input |
| 121 | # Handled Edge Cases: All planets covered with consistent rounding to 2 decimal places |
| 122 | # Handled Edge Cases: Earth method directly returns earth_years for efficiency |