| 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 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 | # Edge Case: Handle zero seconds |
| 38 | if seconds == 0: |
| 39 | self.earth_age = 0 |
| 40 | else: |
| 41 | self.earth_age = self.seconds / self.EARTH_YEAR_SECONDS |
| 42 | |
| 43 | def on_mercury(self) -> float: |
| 44 | """ |
| 45 | Calculate the age on Mercury. |
| 46 | |
| 47 | Returns: |
| 48 | float: The age on Mercury, rounded to two decimal places. |
| 49 | """ |
| 50 | age_on_planet = self.earth_age / self.ORBITAL_PERIODS['mercury'] |
| 51 | return round(age_on_planet, 2) |
| 52 | |
| 53 | def on_venus(self) -> float: |
| 54 | """ |
| 55 | Calculate the age on Venus. |
| 56 | |
| 57 | Returns: |
| 58 | float: The age on Venus, rounded to two decimal places. |
| 59 | """ |
| 60 | age_on_planet = self.earth_age / self.ORBITAL_PERIODS['venus'] |
| 61 | return round(age_on_planet, 2) |
| 62 | |
| 63 | def on_earth(self) -> float: |
| 64 | """ |
| 65 | Calculate the age on Earth. |
| 66 | |
| 67 | Returns: |
| 68 | float: The age on Earth, rounded to two decimal places. |
| 69 | """ |
| 70 | # Edge Case: Directly return the Earth age |
| 71 | return round(self.earth_age, 2) |
| 72 | |
| 73 | def on_mars(self) -> float: |
| 74 | """ |
| 75 | Calculate the age on Mars. |
| 76 | |
| 77 | Returns: |
| 78 | float: The age on Mars, rounded to two decimal places. |
| 79 | """ |
| 80 | age_on_planet = self.earth_age / self.ORBITAL_PERIODS['mars'] |
| 81 | return round(age_on_planet, 2) |
| 82 | |
| 83 | def on_jupiter(self) -> float: |
| 84 | """ |
| 85 | Calculate the age on Jupiter. |
| 86 | |
| 87 | Returns: |
| 88 | float: The age on Jupiter, rounded to two decimal places. |
| 89 | """ |
| 90 | age_on_planet = self.earth_age / self.ORBITAL_PERIODS['jupiter'] |
| 91 | return round(age_on_planet, 2) |
| 92 | |
| 93 | def on_saturn(self) -> float: |
| 94 | """ |
| 95 | Calculate the age on Saturn. |
| 96 | |
| 97 | Returns: |
| 98 | float: The age on Saturn, rounded to two decimal places. |
| 99 | """ |
| 100 | age_on_planet = self.earth_age / self.ORBITAL_PERIODS['saturn'] |
| 101 | return round(age_on_planet, 2) |
| 102 | |
| 103 | def on_uranus(self) -> float: |
| 104 | """ |
| 105 | Calculate the age on Uranus. |
| 106 | |
| 107 | Returns: |
| 108 | float: The age on Uranus, rounded to two decimal places. |
| 109 | """ |
| 110 | age_on_planet = self.earth_age / self.ORBITAL_PERIODS['uranus'] |
| 111 | return round(age_on_planet, 2) |
| 112 | |
| 113 | def on_neptune(self) -> float: |
| 114 | """ |
| 115 | Calculate the age on Neptune. |
| 116 | |
| 117 | Returns: |
| 118 | float: The age on Neptune, rounded to two decimal places. |
| 119 | """ |
| 120 | age_on_planet = self.earth_age / self.ORBITAL_PERIODS['neptune'] |
| 121 | return round(age_on_planet, 2) |
| 122 | |
| 123 | # Handled Edge Cases: Negative seconds, zero seconds, rounding to two decimal places |
| 124 | # Handled Edge Cases: Negative seconds, zero seconds, rounding to two decimal places |
| 125 | # Handled Edge Cases: Negative seconds, zero seconds, rounding to two decimal places |
| 126 | # Handled Edge Cases: Negative seconds, zero seconds, rounding to two decimal places |
| 127 | # Handled Edge Cases: Negative seconds, zero seconds, rounding to two decimal places |
| 128 | # Handled Edge Cases: Negative seconds, zero seconds, rounding to two decimal places |
| 129 | # Handled Edge Cases: Negative seconds, zero seconds, rounding to two decimal places |
| 130 | # Handled Edge Cases: Negative seconds, zero seconds, rounding to two decimal places |