| 1 | 1 | class SpaceAge: |
| 2 | """ |
| 3 | A class to calculate a person's age 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 |
| 7 | the equivalent age on each planet, accounting for that planet's orbital period. |
| 8 | |
| 9 | Args: |
| 10 | seconds (int): 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 an age in seconds. |
| 31 | |
| 32 | Args: |
| 33 | seconds (int): Age in seconds |
| 34 | """ |
| 35 | # Edge Case: Handle negative seconds by taking absolute value |
| 36 | self.seconds = abs(seconds) if seconds < 0 else seconds |
| 37 | |
| 38 | # Edge Case: Handle non-integer input by converting to int |
| 39 | if not isinstance(seconds, int): |
| 40 | self.seconds = int(self.seconds) |
| 41 | |
| 42 | def _calculate_age_on_planet(self, planet: str) -> float: |
| 43 | """ |
| 44 | Helper method to calculate age on a specific planet. |
| 45 | |
| 46 | Args: |
| 47 | planet (str): Name of the planet |
| 48 | |
| 49 | Returns: |
| 50 | float: Age on the specified planet, rounded to 2 decimal places |
| 51 | """ |
| 52 | # Calculate Earth years |
| 53 | earth_years = self.seconds / self.EARTH_YEAR_SECONDS |
| 54 | |
| 55 | # Calculate age on the specified planet |
| 56 | planet_age = earth_years / self.ORBITAL_PERIODS[planet] |
| 57 | |
| 58 | # Round to 2 decimal places |
| 59 | return round(planet_age, 2) |
| 60 | |
| 61 | def on_mercury(self) -> float: |
| 62 | """ |
| 63 | Calculate age on Mercury. |
| 64 | |
| 65 | Returns: |
| 66 | float: Age on Mercury in Mercury years, rounded to 2 decimal places |
| 67 | """ |
| 68 | return self._calculate_age_on_planet('mercury') |
| 69 | |
| 70 | def on_venus(self) -> float: |
| 71 | """ |
| 72 | Calculate age on Venus. |
| 73 | |
| 74 | Returns: |
| 75 | float: Age on Venus in Venus years, rounded to 2 decimal places |
| 76 | """ |
| 77 | return self._calculate_age_on_planet('venus') |
| 78 | |
| 79 | def on_earth(self) -> float: |
| 80 | """ |
| 81 | Calculate age on Earth. |
| 82 | |
| 83 | Returns: |
| 84 | float: Age on Earth in Earth years, rounded to 2 decimal places |
| 85 | """ |
| 86 | return self._calculate_age_on_planet('earth') |
| 87 | |
| 88 | def on_mars(self) -> float: |
| 89 | """ |
| 90 | Calculate age on Mars. |
| 91 | |
| 92 | Returns: |
| 93 | float: Age on Mars in Mars years, rounded to 2 decimal places |
| 94 | """ |
| 95 | return self._calculate_age_on_planet('mars') |
| 96 | |
| 97 | def on_jupiter(self) -> float: |
| 98 | """ |
| 99 | Calculate age on Jupiter. |
| 100 | |
| 101 | Returns: |
| 102 | float: Age on Jupiter in Jupiter years, rounded to 2 decimal places |
| 103 | """ |
| 104 | return self._calculate_age_on_planet('jupiter') |
| 105 | |
| 106 | def on_saturn(self) -> float: |
| 107 | """ |
| 108 | Calculate age on Saturn. |
| 109 | |
| 110 | Returns: |
| 111 | float: Age on Saturn in Saturn years, rounded to 2 decimal places |
| 112 | """ |
| 113 | return self._calculate_age_on_planet('saturn') |
| 114 | |
| 115 | def on_uranus(self) -> float: |
| 116 | """ |
| 117 | Calculate age on Uranus. |
| 118 | |
| 119 | Returns: |
| 120 | float: Age on Uranus in Uranus years, rounded to 2 decimal places |
| 121 | """ |
| 122 | return self._calculate_age_on_planet('uranus') |
| 123 | |
| 124 | def on_neptune(self) -> float: |
| 125 | """ |
| 126 | Calculate age on Neptune. |
| 127 | |
| 128 | Returns: |
| 129 | float: Age on Neptune in Neptune years, rounded to 2 decimal places |
| 130 | """ |
| 131 | return self._calculate_age_on_planet('neptune') |
| 132 | |
| 133 | # Handled Edge Cases: Negative seconds, Non-integer input |
| 134 | |
| 135 | # Handled Edge Cases: Negative seconds, Non-integer input |