| 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: 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: The age on Mercury in Earth years, rounded to two 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: The age on Venus in Earth years, rounded to two 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: The age on Earth in Earth years, rounded to two decimal places |
| 68 | """ |
| 69 | # Edge Case: Zero seconds input |
| 70 | if self.seconds == 0: |
| 71 | return 0.0 |
| 72 | |
| 73 | age_on_planet = self.earth_years / self.ORBITAL_PERIODS['earth'] |
| 74 | return round(age_on_planet, 2) |
| 75 | |
| 76 | def on_mars(self) -> float: |
| 77 | """ |
| 78 | Calculate the age on Mars. |
| 79 | |
| 80 | Returns: |
| 81 | float: The age on Mars in Earth years, rounded to two decimal places |
| 82 | """ |
| 83 | age_on_planet = self.earth_years / self.ORBITAL_PERIODS['mars'] |
| 84 | return round(age_on_planet, 2) |
| 85 | |
| 86 | def on_jupiter(self) -> float: |
| 87 | """ |
| 88 | Calculate the age on Jupiter. |
| 89 | |
| 90 | Returns: |
| 91 | float: The age on Jupiter in Earth years, rounded to two decimal places |
| 92 | """ |
| 93 | age_on_planet = self.earth_years / self.ORBITAL_PERIODS['jupiter'] |
| 94 | return round(age_on_planet, 2) |
| 95 | |
| 96 | def on_saturn(self) -> float: |
| 97 | """ |
| 98 | Calculate the age on Saturn. |
| 99 | |
| 100 | Returns: |
| 101 | float: The age on Saturn in Earth years, rounded to two decimal places |
| 102 | """ |
| 103 | age_on_planet = self.earth_years / self.ORBITAL_PERIODS['saturn'] |
| 104 | return round(age_on_planet, 2) |
| 105 | |
| 106 | def on_uranus(self) -> float: |
| 107 | """ |
| 108 | Calculate the age on Uranus. |
| 109 | |
| 110 | Returns: |
| 111 | float: The age on Uranus in Earth years, rounded to two decimal places |
| 112 | """ |
| 113 | age_on_planet = self.earth_years / self.ORBITAL_PERIODS['uranus'] |
| 114 | return round(age_on_planet, 2) |
| 115 | |
| 116 | def on_neptune(self) -> float: |
| 117 | """ |
| 118 | Calculate the age on Neptune. |
| 119 | |
| 120 | Returns: |
| 121 | float: The age on Neptune in Earth years, rounded to two decimal places |
| 122 | """ |
| 123 | age_on_planet = self.earth_years / self.ORBITAL_PERIODS['neptune'] |
| 124 | return round(age_on_planet, 2) |
| 125 | |
| 126 | # Handled Edge Cases: Negative seconds input, Zero seconds input |
| 127 | # Handled Edge Cases: Zero seconds input |
| 128 | # Handled Edge Cases: Zero seconds input |
| 129 | # Handled Edge Cases: Zero seconds input |
| 130 | # Handled Edge Cases: Zero seconds input |
| 131 | # Handled Edge Cases: Zero seconds input |
| 132 | # Handled Edge Cases: Zero seconds input |
| 133 | # Handled Edge Cases: Zero seconds input |