| 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 provided, converting it to years |
| 6 | according to the orbital periods of various planets relative to Earth. |
| 7 | |
| 8 | Attributes: |
| 9 | seconds (int): The age in seconds. |
| 10 | earth_year_in_seconds (float): The number of seconds in one Earth year (31,557,600). |
| 11 | """ |
| 12 | |
| 13 | # Define the number of seconds in one Earth year |
| 14 | EARTH_YEAR_IN_SECONDS = 31557600 |
| 15 | |
| 16 | # Define the orbital periods of planets relative to 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 number of seconds. |
| 31 | |
| 32 | Args: |
| 33 | seconds (int): The age in seconds. |
| 34 | |
| 35 | # Edge Case: Negative seconds input |
| 36 | # Edge Case: Zero seconds input |
| 37 | """ |
| 38 | # Edge Case: Negative seconds input |
| 39 | if seconds < 0: |
| 40 | raise ValueError("Seconds cannot be negative") |
| 41 | |
| 42 | # Edge Case: Zero seconds input |
| 43 | if seconds == 0: |
| 44 | self.seconds = 0 |
| 45 | else: |
| 46 | self.seconds = seconds |
| 47 | |
| 48 | def _calculate_age_on_planet(self, planet: str) -> float: |
| 49 | """ |
| 50 | Calculate the age on a specific planet. |
| 51 | |
| 52 | Args: |
| 53 | planet (str): The name of the planet (lowercase). |
| 54 | |
| 55 | Returns: |
| 56 | float: The age on the specified planet, rounded to 2 decimal places. |
| 57 | |
| 58 | # Edge Case: Invalid planet name |
| 59 | """ |
| 60 | # Edge Case: Invalid planet name |
| 61 | if planet not in self.ORBITAL_PERIODS: |
| 62 | raise ValueError(f"Invalid planet name: {planet}") |
| 63 | |
| 64 | # Calculate the age in Earth years |
| 65 | earth_years = self.seconds / self.EARTH_YEAR_IN_SECONDS |
| 66 | |
| 67 | # Convert to the planet's years |
| 68 | planet_years = earth_years / self.ORBITAL_PERIODS[planet] |
| 69 | |
| 70 | # Round to 2 decimal places |
| 71 | return round(planet_years, 2) |
| 72 | |
| 73 | def on_mercury(self) -> float: |
| 74 | """ |
| 75 | Calculate the age on Mercury. |
| 76 | |
| 77 | Returns: |
| 78 | float: The age on Mercury, rounded to 2 decimal places. |
| 79 | """ |
| 80 | return self._calculate_age_on_planet('mercury') |
| 81 | |
| 82 | def on_venus(self) -> float: |
| 83 | """ |
| 84 | Calculate the age on Venus. |
| 85 | |
| 86 | Returns: |
| 87 | float: The age on Venus, rounded to 2 decimal places. |
| 88 | """ |
| 89 | return self._calculate_age_on_planet('venus') |
| 90 | |
| 91 | def on_earth(self) -> float: |
| 92 | """ |
| 93 | Calculate the age on Earth. |
| 94 | |
| 95 | Returns: |
| 96 | float: The age on Earth, rounded to 2 decimal places. |
| 97 | """ |
| 98 | return self._calculate_age_on_planet('earth') |
| 99 | |
| 100 | def on_mars(self) -> float: |
| 101 | """ |
| 102 | Calculate the age on Mars. |
| 103 | |
| 104 | Returns: |
| 105 | float: The age on Mars, rounded to 2 decimal places. |
| 106 | """ |
| 107 | return self._calculate_age_on_planet('mars') |
| 108 | |
| 109 | def on_jupiter(self) -> float: |
| 110 | """ |
| 111 | Calculate the age on Jupiter. |
| 112 | |
| 113 | Returns: |
| 114 | float: The age on Jupiter, rounded to 2 decimal places. |
| 115 | """ |
| 116 | return self._calculate_age_on_planet('jupiter') |
| 117 | |
| 118 | def on_saturn(self) -> float: |
| 119 | """ |
| 120 | Calculate the age on Saturn. |
| 121 | |
| 122 | Returns: |
| 123 | float: The age on Saturn, rounded to 2 decimal places. |
| 124 | """ |
| 125 | return self._calculate_age_on_planet('saturn') |
| 126 | |
| 127 | def on_uranus(self) -> float: |
| 128 | """ |
| 129 | Calculate the age on Uranus. |
| 130 | |
| 131 | Returns: |
| 132 | float: The age on Uranus, rounded to 2 decimal places. |
| 133 | """ |
| 134 | return self._calculate_age_on_planet('uranus') |
| 135 | |
| 136 | def on_neptune(self) -> float: |
| 137 | """ |
| 138 | Calculate the age on Neptune. |
| 139 | |
| 140 | Returns: |
| 141 | float: The age on Neptune, rounded to 2 decimal places. |
| 142 | """ |
| 143 | return self._calculate_age_on_planet('neptune') |
| 144 | |
| 145 | # Handled Edge Cases: Negative seconds input, Zero seconds input, Invalid planet name |
| 146 | # Handled Edge Cases: Negative seconds input, Zero seconds input |
| 147 | # Handled Edge Cases: Invalid planet name |
| 148 | # Handled Edge Cases: Invalid planet name |
| 149 | # Handled Edge Cases: Invalid planet name |
| 150 | # Handled Edge Cases: Invalid planet name |
| 151 | # Handled Edge Cases: Invalid planet name |
| 152 | # Handled Edge Cases: Invalid planet name |
| 153 | # Handled Edge Cases: Invalid planet name |
| 154 | # Handled Edge Cases: Invalid planet name |