Avatar of artemkorsakov

artemkorsakov's solution

to Space Age in the C# Track

Published at Feb 20 2019 · 0 comments
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.

Running the tests

To run the tests, run the command dotnet test from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the Skip property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using exercism submit SpaceAge.cs

Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language 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.

SpaceAgeTest.cs

// This file was auto-generated based on version 1.2.0 of the canonical data.

using Xunit;

public class SpaceAgeTest
{
    [Fact]
    public void Age_on_earth()
    {
        var sut = new SpaceAge(1000000000);
        Assert.Equal(31.69, sut.OnEarth(), precision: 2);
    }

    [Fact(Skip = "Remove to run test")]
    public void Age_on_mercury()
    {
        var sut = new SpaceAge(2134835688);
        Assert.Equal(280.88, sut.OnMercury(), precision: 2);
    }

    [Fact(Skip = "Remove to run test")]
    public void Age_on_venus()
    {
        var sut = new SpaceAge(189839836);
        Assert.Equal(9.78, sut.OnVenus(), precision: 2);
    }

    [Fact(Skip = "Remove to run test")]
    public void Age_on_mars()
    {
        var sut = new SpaceAge(2129871239);
        Assert.Equal(35.88, sut.OnMars(), precision: 2);
    }

    [Fact(Skip = "Remove to run test")]
    public void Age_on_jupiter()
    {
        var sut = new SpaceAge(901876382);
        Assert.Equal(2.41, sut.OnJupiter(), precision: 2);
    }

    [Fact(Skip = "Remove to run test")]
    public void Age_on_saturn()
    {
        var sut = new SpaceAge(2000000000);
        Assert.Equal(2.15, sut.OnSaturn(), precision: 2);
    }

    [Fact(Skip = "Remove to run test")]
    public void Age_on_uranus()
    {
        var sut = new SpaceAge(1210123456);
        Assert.Equal(0.46, sut.OnUranus(), precision: 2);
    }

    [Fact(Skip = "Remove to run test")]
    public void Age_on_neptune()
    {
        var sut = new SpaceAge(1821023456);
        Assert.Equal(0.35, sut.OnNeptune(), precision: 2);
    }
}
using System;

/// <summary>
/// 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
/// </summary>
public class SpaceAge
{
    private const int SecondsInHours = 3600;
    private const int HoursInDay = 24;
    private const double DaysInYear = 365.25;
    private const double MercuryOrbitalPeriod = 0.2408467;
    private const double VenusOrbitalPeriod = 0.61519726;
    private const double MarsOrbitalPeriod = 1.8808158;
    private const double JupiterOrbitalPeriod = 11.862615;
    private const double SaturnOrbitalPeriod = 29.447498;
    private const double UranusOrbitalPeriod = 84.016846;
    private const double NeptuneOrbitalPeriod = 164.79132;
    private readonly double _seconds;

    public SpaceAge(int seconds)
    {
        _seconds = seconds;
    }

    // One Earth year is (SecondsInHours * HoursInDay * DaysInYear) seconds
    public double OnEarth()
    {
        return _seconds / (SecondsInHours * HoursInDay * DaysInYear);
    }

    // - Mercury: orbital period 0.2408467 Earth years
    public double OnMercury()
    {
        return OnEarth() / MercuryOrbitalPeriod;
    }
    
    // - Venus: orbital period 0.61519726 Earth years
    public double OnVenus()
    {
        return OnEarth() / VenusOrbitalPeriod;
    }

    // - Mars: orbital period 1.8808158 Earth years
    public double OnMars()
    {
        return OnEarth() / MarsOrbitalPeriod;
    }

    // - Jupiter: orbital period 11.862615 Earth years
    public double OnJupiter()
    {
        return OnEarth() / JupiterOrbitalPeriod;
    }

    // - Saturn: orbital period 29.447498 Earth years
    public double OnSaturn()
    {
        return OnEarth() / SaturnOrbitalPeriod;
    }

    // - Uranus: orbital period 84.016846 Earth years
    public double OnUranus()
    {
        return OnEarth() / UranusOrbitalPeriod;
    }

    // - Neptune: orbital period 164.79132 Earth years
    public double OnNeptune()
    {
        return OnEarth() / NeptuneOrbitalPeriod;
    }
}

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?