Skip to content

Instantly share code, notes, and snippets.

@schmonz
Last active February 3, 2018 18:09
Show Gist options
  • Save schmonz/ea973425f277fc30398538917b3f61ca to your computer and use it in GitHub Desktop.
Save schmonz/ea973425f277fc30398538917b3f61ca to your computer and use it in GitHub Desktop.
files attached to bug report for Exercism.io Perl5 track

Space Age

Write a program that, given an age in seconds, calculates how old someone is in terms of a given planet's solar years.

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 Earth-years old.

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

Source

Partially inspired by Chapter 1 in Chris Pine's online Learn to Program tutorial. view source

use strict;
use warnings;
use Test::More;
my $module = $ENV{EXERCISM} ? 'Example' : 'SpaceAge';
plan tests => 20;
ok -e "$module.pm", "Missing $module.pm"
or BAIL_OUT "You need to create file: $module.pm";
eval "use $module";
ok !$@, "Cannot load $module"
or BAIL_OUT "Cannot load $module; Does it compile? Does it end with 1;?";
can_ok $module, "new" or BAIL_OUT "Missing package $module; or missing sub new()";
can_ok $module, "seconds" or BAIL_OUT "Missing package $module; or missing sub seconds()";
is $module->new(1_000_000)->seconds, 1_000_000, "attribute->seconds match constructor param";
is $module->new(1_000_000_000)->on_earth, 31.69, "earth years";
is $module->new(2_134_835_688)->on_earth, 67.65, "earth years";
is $module->new(2_134_835_688)->on_mercury, 280.88, "mercury years";
is $module->new(189_839_836)->on_earth, 6.02, "earth years";
is $module->new(189_839_836)->on_venus, 9.78, "venus years";
is $module->new(2_329_871_239)->on_earth, 73.83, "earth years";
is $module->new(2_329_871_239)->on_mars, 39.25, "mars years";
is $module->new(901_876_382)->on_earth, 28.58, "earth years";
is $module->new(901_876_382)->on_jupiter, 2.41, "jupiter years";
is $module->new(3_000_000_000)->on_earth, 95.06, "earth years";
is $module->new(3_000_000_000)->on_saturn, 3.23, "saturn years";
is $module->new(3_210_123_456)->on_earth, 101.72, "earth years";
is $module->new(3_210_123_456)->on_uranus, 1.21, "uranus years";
is $module->new(8_210_123_456)->on_earth, 260.16, "earth years";
is $module->new(8_210_123_456)->on_neptune, 1.58, "neptune years";
package SpaceAge;
use strict;
use warnings;
sub new {
my ($class, $seconds) = @_;
my $self = {};
$self->{seconds} = $seconds;
return bless($self, $class);
}
sub seconds {
my ($self) = @_;
return $self->{seconds};
}
sub earth_years {
my ($seconds) = @_;
my $minutes = $seconds / 60;
my $hours = $minutes / 60;
my $days = $hours / 24;
my $years = $days / 365.25;
return $years;
}
sub round_to_two_decimal_places {
my ($number) = @_;
return sprintf("%.2f", $number);
}
sub on_earth {
my ($self) = @_;
return round_to_two_decimal_places(earth_years($self->{seconds}));
}
sub on_mercury {
my ($self) = @_;
my $mercury_years = earth_years($self->{seconds}) / 0.2408467;
return round_to_two_decimal_places($mercury_years);
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment