Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of rootulp

rootulp's solution

to Space Age in the Python Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Given an age in seconds, calculate how old someone would be on:

  • Earth: orbital period 365.25 Earth days, or 31557600 seconds
  • Mercury: orbital period 0.2408467 Earth years
  • Venus: orbital period 0.61519726 Earth years
  • Mars: orbital period 1.8808158 Earth years
  • Jupiter: orbital period 11.862615 Earth years
  • Saturn: orbital period 29.447498 Earth years
  • Uranus: orbital period 84.016846 Earth years
  • Neptune: orbital period 164.79132 Earth years

So if you were told someone were 1,000,000,000 seconds old, you should be able to say that they're 31.69 Earth-years old.

If you're wondering why Pluto didn't make the cut, go watch this youtube video.

Exception messages

Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include a message.

To raise a message with an exception, just write it as an argument to the exception type. For example, instead of raise Exception, you should write:

raise Exception("Meaningful message indicating the source of the error")

Running the tests

To run the tests, run the appropriate command below (why they are different):

  • Python 2.7: py.test space_age_test.py
  • Python 3.4+: pytest space_age_test.py

Alternatively, you can tell Python to run the pytest module (allowing the same command to be used regardless of Python version): python -m pytest space_age_test.py

Common pytest options

  • -v : enable verbose output
  • -x : stop running tests on first failure
  • --ff : run failures from previous test before running other test cases

For other options, see python -m pytest -h

Submitting Exercises

Note that, when trying to submit an exercise, make sure the solution is in the $EXERCISM_WORKSPACE/python/space-age directory.

You can find your Exercism workspace by running exercism debug and looking for the line that starts with Workspace.

For more detailed information about running tests, code style and linting, please see the help page.

Source

Partially inspired by Chapter 1 in Chris Pine's online Learn to Program tutorial. http://pine.fm/LearnToProgram/?Chapter=01

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

space_age_test.py

import unittest

from space_age import SpaceAge


# Tests adapted from `problem-specifications//canonical-data.json` @ v1.1.0

class SpaceAgeTest(unittest.TestCase):

    def test_age_on_mercury(self):
        self.assertEqual(SpaceAge(2134835688).on_mercury(), 280.88)

    def test_age_on_venus(self):
        self.assertEqual(SpaceAge(189839836).on_venus(), 9.78)

    def test_age_on_earth(self):
        self.assertEqual(SpaceAge(1000000000).on_earth(), 31.69)

    def test_age_on_mars(self):
        self.assertEqual(SpaceAge(2329871239).on_mars(), 39.25)

    def test_age_on_jupiter(self):
        self.assertEqual(SpaceAge(901876382).on_jupiter(), 2.41)

    def test_age_on_saturn(self):
        self.assertEqual(SpaceAge(3000000000).on_saturn(), 3.23)

    def test_age_on_uranus(self):
        self.assertEqual(SpaceAge(3210123456).on_uranus(), 1.21)

    def test_age_on_neptune(self):
        self.assertEqual(SpaceAge(8210123456).on_neptune(), 1.58)

    # Additional tests for this track

    def test_age_in_seconds(self):
        self.assertEqual(SpaceAge(1e6).seconds, 1e6)


if __name__ == '__main__':
    unittest.main()
class SpaceAge:

    SECONDS_IN_YEAR = 31557600.0
    ORBITAL_PERIODS = {
        'mercury': 0.2408467,
        'venus':   0.61519726,
        'earth':   1,
        'mars':    1.8808158,
        'jupiter': 11.862615,
        'saturn':  29.447498,
        'uranus':  84.016846,
        'neptune': 164.79132
    }

    def __init__(self, seconds):
        self.seconds = seconds

    def to_earth(self):
        return self.seconds / self.SECONDS_IN_YEAR

    def on_planet(self, planet):
        return round(self.to_earth() / self.ORBITAL_PERIODS[planet], 2)

    def on_mercury(self):
        return self.on_planet('mercury')

    def on_venus(self):
        return self.on_planet('venus')

    def on_earth(self):
        return self.on_planet('earth')

    def on_mars(self):
        return self.on_planet('mars')

    def on_jupiter(self):
        return self.on_planet('jupiter')

    def on_saturn(self):
        return self.on_planet('saturn')

    def on_uranus(self):
        return self.on_planet('uranus')

    def on_neptune(self):
        return self.on_planet('neptune')

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other peopleโ€™s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?