| 1 | 1 | class SpaceAge: |
| 2 | """ |
| 3 | A class to calculate the age of a person on different planets in our Solar System. |
| 4 | |
| 5 | The age is calculated based on the number of seconds passed since birth. |
| 6 | Each method returns the age on a specific planet, rounded to two decimal places. |
| 7 | |
| 8 | Args: |
| 9 | seconds (int): The age in seconds. |
| 10 | """ |
| 11 | |
| 12 | # Earth year in seconds |
| 13 | EARTH_YEAR_SECONDS = 31557600 |
| 14 | |
| 15 | # Orbital periods relative to Earth years |
| 16 | ORBITAL_PERIODS = { |
| 17 | 'mercury': 0.2408467, |
| 18 | 'venus': 0.61519726, |
| 19 | 'earth': 1.0, |
| 20 | 'mars': 1.8808158, |
| 21 | 'jupiter': 11.862615, |
| 22 | 'saturn': 29.447498, |
| 23 | 'uranus': 84.016846, |
| 24 | 'neptune': 164.79132 |
| 25 | } |
| 26 | |
| 2 | 27 | def __init__(self, seconds: int) -> None: |
| 3 | | pass |
| 28 | """ |
| 29 | Initialize the SpaceAge object with the given number of seconds. |
| 30 | |
| 31 | Args: |
| 32 | seconds (int): The age in seconds. |
| 33 | """ |
| 34 | # Edge Case: Negative seconds input |
| 35 | if seconds < 0: |
| 36 | raise ValueError("Seconds cannot be negative") |
| 37 | |
| 38 | self.seconds = seconds |
| 39 | # Calculate Earth years once to avoid repeated calculations |
| 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: The age on Mercury, rounded to two decimal places. |
| 48 | """ |
| 49 | age = self.earth_years / self.ORBITAL_PERIODS['mercury'] |
| 50 | return round(age, 2) |
| 51 | |
| 52 | def on_venus(self) -> float: |
| 53 | """ |
| 54 | Calculate the age on Venus. |
| 55 | |
| 56 | Returns: |
| 57 | float: The age on Venus, rounded to two decimal places. |
| 58 | """ |
| 59 | age = self.earth_years / self.ORBITAL_PERIODS['venus'] |
| 60 | return round(age, 2) |
| 61 | |
| 62 | def on_earth(self) -> float: |
| 63 | """ |
| 64 | Calculate the age on Earth. |
| 65 | |
| 66 | Returns: |
| 67 | float: The age on Earth, rounded to two decimal places. |
| 68 | """ |
| 69 | # Edge Case: Direct calculation for Earth (period is 1.0) |
| 70 | age = self.earth_years |
| 71 | return round(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 = self.earth_years / self.ORBITAL_PERIODS['mars'] |
| 81 | return round(age, 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 = self.earth_years / self.ORBITAL_PERIODS['jupiter'] |
| 91 | return round(age, 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 = self.earth_years / self.ORBITAL_PERIODS['saturn'] |
| 101 | return round(age, 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 = self.earth_years / self.ORBITAL_PERIODS['uranus'] |
| 111 | return round(age, 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 = self.earth_years / self.ORBITAL_PERIODS['neptune'] |
| 121 | return round(age, 2) |
| 122 | |
| 123 | # Handled Edge Cases: Negative seconds input, Direct calculation for Earth (period is 1.0) |