🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of jac33k

jac33k's solution

to Space Age in the Raku Track

Published at Mar 18 2019 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution 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.

Resources

Remember to check out the Perl 6 documentation and resources pages for information, tips, and examples if you get stuck.

Running the tests

There is a test suite and module included with the exercise. The test suite (a file with the extension .t) will attempt to run routines from the module (a file with the extension .pm6). Add/modify routines in the module so that the tests will pass! You can view the test data by executing the command perl6 --doc *.t (* being the name of the test suite), and run the test suite for the exercise by executing the command prove6 . in the exercise directory.

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.

unit module SpaceAge;

role Planet {
  method age-on ($seconds) {
    ($seconds / self.orbital-period).round(1e-2);
  }
}

class Earth does Planet is export {
  my $.orbital-period = 31557600;
}

class Mercury does Planet is export {
  my $.orbital-period = (31557600 * 0.2408467);
}

class Venus does Planet is export {
  my $.orbital-period = (31557600 * 0.61519726);
}

class Mars does Planet is export {
  my $.orbital-period = (31557600 * 1.8808158);
}

class Jupiter does Planet is export {
  my $.orbital-period = (31557600 * 11.862615);
}

class Saturn does Planet is export {
  my $.orbital-period = (31557600 * 29.447498);
}

class Uranus does Planet is export {
  my $.orbital-period = (31557600 * 84.016846);
}

class Neptune does Planet is export {
  my $.orbital-period = (31557600 * 164.79132);
}


=for comment
---
Orbital periods relative to Earth:
  Mercury: 0.2408467
  Venus: 0.61519726
  Mars: 1.8808158
  Jupiter: 11.862615
  Saturn: 29.447498
  Uranus: 84.016846
  Neptune: 164.79132
...

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?