Avatar of 4d47
0
0
Genius
0
1

4d47's solution

to Space Age in the Perl 6 Track

0
0
Genius
0
1
Instructions
Test suite
Solution

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.

space-age.t

#!/usr/bin/env perl6
use v6;
use Test;
use JSON::Fast;
use lib $?FILE.IO.dirname;
use SpaceAge;
plan 8;

my $c-data = from-json $=pod.pop.contents;
is (age-on ::(.<input><planet>): .<input><seconds>), |.<expected description> for @($c-data<cases>);

=head2 Canonical Data
=begin code
{
  "exercise": "space-age",
  "version": "1.1.0",
  "cases": [
    {
      "description": "age on Earth",
      "property": "age",
      "input": {
        "planet": "Earth",
        "seconds": 1000000000
      },
      "expected": 31.69
    },
    {
      "description": "age on Mercury",
      "property": "age",
      "input": {
        "planet": "Mercury",
        "seconds": 2134835688
      },
      "expected": 280.88
    },
    {
      "description": "age on Venus",
      "property": "age",
      "input": {
        "planet": "Venus",
        "seconds": 189839836
      },
      "expected": 9.78
    },
    {
      "description": "age on Mars",
      "property": "age",
      "input": {
        "planet": "Mars",
        "seconds": 2329871239
      },
      "expected": 39.25
    },
    {
      "description": "age on Jupiter",
      "property": "age",
      "input": {
        "planet": "Jupiter",
        "seconds": 901876382
      },
      "expected": 2.41
    },
    {
      "description": "age on Saturn",
      "property": "age",
      "input": {
        "planet": "Saturn",
        "seconds": 3000000000
      },
      "expected": 3.23
    },
    {
      "description": "age on Uranus",
      "property": "age",
      "input": {
        "planet": "Uranus",
        "seconds": 3210123456
      },
      "expected": 1.21
    },
    {
      "description": "age on Neptune",
      "property": "age",
      "input": {
        "planet": "Neptune",
        "seconds": 8210123456
      },
      "expected": 1.58
    }
  ]
}
=end code
#| Earth orbital period in seconds
constant P = 31557600;

constant orbital-periods = {
    :Earth(1 * P),
    :Mercury(0.2408467 * P),
    :Venus(0.61519726 * P),
    :Mars(1.8808158 * P),
    :Jupiter(11.862615 * P),
    :Saturn(29.447498 * P),
    :Uranus(84.016846 * P),
    :Neptune(164.79132 * P)
};


sub age-on(Str $planet, UInt $seconds --> Real) is export {
    ($seconds / orbital-periods{$planet}).round(0.01)
}

What can you learn from this solution?

A huge amount can be learnt 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 I could read more about to develop my understanding?

Community comments

See what others have said about this solution
almost 2 years ago
4d47 says

At doc for P and age-on return constraint