| 1 | 1 | class SpaceAge: |
| 2 | # Earth year in seconds |
| 3 | EARTH_YEAR_IN_SECONDS = 31557600 |
| 4 | |
| 5 | # Orbital periods in Earth years |
| 6 | ORBITAL_PERIODS = { |
| 7 | 'mercury': 0.2408467, |
| 8 | 'venus': 0.61519726, |
| 9 | 'earth': 1.0, |
| 10 | 'mars': 1.8808158, |
| 11 | 'jupiter': 11.862615, |
| 12 | 'saturn': 29.447498, |
| 13 | 'uranus': 84.016846, |
| 14 | 'neptune': 164.79132 |
| 15 | } |
| 16 | |
| 2 | 17 | def __init__(self, seconds: int) -> None: |
| 3 | | pass |
| 18 | """ |
| 19 | Initialize the SpaceAge object with the given age in seconds. |
| 20 | |
| 21 | Parameters: |
| 22 | seconds (int): The age in seconds. Must be non-negative. |
| 23 | |
| 24 | Raises: |
| 25 | ValueError: If seconds is negative. |
| 26 | """ |
| 27 | # Edge Case: Negative seconds input |
| 28 | if seconds < 0: |
| 29 | raise ValueError("Seconds cannot be negative") |
| 30 | |
| 31 | self.seconds = seconds |
| 32 | |
| 33 | # Create methods dynamically for each planet |
| 34 | for planet, period in self.ORBITAL_PERIODS.items(): |
| 35 | # Create a closure to capture the period value |
| 36 | def make_method(orbital_period): |
| 37 | def method(): |
| 38 | earth_years = self.seconds / self.EARTH_YEAR_IN_SECONDS |
| 39 | planet_years = earth_years / orbital_period |
| 40 | return round(planet_years, 2) |
| 41 | return method |
| 42 | |
| 43 | setattr(self, f'on_{planet}', make_method(period)) |
| 44 | |
| 45 | # Handled Edge Cases: Negative seconds input |
| 46 | |
| 47 | def on_mercury(self) -> float: |
| 48 | """ |
| 49 | Calculate the age on Mercury. |
| 50 | |
| 51 | Returns: |
| 52 | float: The age on Mercury in years, rounded to 2 decimal places. |
| 53 | """ |
| 54 | pass # Implemented dynamically in __init__ |
| 55 | |
| 56 | def on_venus(self) -> float: |
| 57 | """ |
| 58 | Calculate the age on Venus. |
| 59 | |
| 60 | Returns: |
| 61 | float: The age on Venus in years, rounded to 2 decimal places. |
| 62 | """ |
| 63 | pass # Implemented dynamically in __init__ |
| 64 | |
| 65 | def on_earth(self) -> float: |
| 66 | """ |
| 67 | Calculate the age on Earth. |
| 68 | |
| 69 | Returns: |
| 70 | float: The age on Earth in years, rounded to 2 decimal places. |
| 71 | """ |
| 72 | pass # Implemented dynamically in __init__ |
| 73 | |
| 74 | def on_mars(self) -> float: |
| 75 | """ |
| 76 | Calculate the age on Mars. |
| 77 | |
| 78 | Returns: |
| 79 | float: The age on Mars in years, rounded to 2 decimal places. |
| 80 | """ |
| 81 | pass # Implemented dynamically in __init__ |
| 82 | |
| 83 | def on_jupiter(self) -> float: |
| 84 | """ |
| 85 | Calculate the age on Jupiter. |
| 86 | |
| 87 | Returns: |
| 88 | float: The age on Jupiter in years, rounded to 2 decimal places. |
| 89 | """ |
| 90 | pass # Implemented dynamically in __init__ |
| 91 | |
| 92 | def on_saturn(self) -> float: |
| 93 | """ |
| 94 | Calculate the age on Saturn. |
| 95 | |
| 96 | Returns: |
| 97 | float: The age on Saturn in years, rounded to 2 decimal places. |
| 98 | """ |
| 99 | pass # Implemented dynamically in __init__ |
| 100 | |
| 101 | def on_uranus(self) -> float: |
| 102 | """ |
| 103 | Calculate the age on Uranus. |
| 104 | |
| 105 | Returns: |
| 106 | float: The age on Uranus in years, rounded to 2 decimal places. |
| 107 | """ |
| 108 | pass # Implemented dynamically in __init__ |
| 109 | |
| 110 | def on_neptune(self) -> float: |
| 111 | """ |
| 112 | Calculate the age on Neptune. |
| 113 | |
| 114 | Returns: |
| 115 | float: The age on Neptune in years, rounded to 2 decimal places. |
| 116 | """ |
| 117 | pass # Implemented dynamically in __init__ |
| 118 | |
| 119 | # Handled Edge Cases: Negative seconds input |