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

JaeHyoLee's solution

to Space Age in the Lua Track

Published at Jul 13 2018 · 0 comments
Test suite

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 busted from within the exercise directory.

Further information

For more detailed information about the Lua track, including how to get help if you're having trouble, please visit the exercism.io Lua language page.


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.


local SpaceAge = require('space-age')

describe('space-age', function()
  it('age in seconds', function()
    local age = SpaceAge:new(1000000)
    assert.are.equal(1000000, age.seconds)

  it('age in Earth years', function()
    local age = SpaceAge:new(1000000000)
    assert.are.equal(31.69, age.on_earth())

  it('age in Mercury years', function()
    local age = SpaceAge:new(2134835688)
    assert.are.equal(67.65, age.on_earth())
    assert.are.equal(280.88, age.on_mercury())

  it('age in Venus years', function()
    local age = SpaceAge:new(189839836)
    assert.are.equal(6.02, age.on_earth())
    assert.are.equal(9.78, age.on_venus())

  it('age in Mars years', function()
    local age = SpaceAge:new(2329871239)
    assert.are.equal(73.83, age.on_earth())
    assert.are.equal(39.25, age.on_mars())

  it('age in Jupiter years', function()
    local age = SpaceAge:new(901876382)
    assert.are.equal(28.58, age.on_earth())
    assert.are.equal(2.41, age.on_jupiter())

  it('age in Saturn years', function()
    local age = SpaceAge:new(3000000000)
    assert.are.equal(95.06, age.on_earth())
    assert.are.equal(3.23, age.on_saturn())

  it('age in Uranus years', function()
    local age = SpaceAge:new(3210123456)
    assert.are.equal(101.72, age.on_earth())
    assert.are.equal(1.21, age.on_uranus())

  it('age in Neptune year', function()
    local age = SpaceAge:new(8210123456)
    assert.are.equal(260.16, age.on_earth())
    assert.are.equal(1.58, age.on_neptune())
local space_age = {}

function space_age:new(seconds)

  local function calculator(earth_year)
    return function() return tonumber(string.format("%.2f", seconds/ 31557600 / earth_year)) end

  return {
    seconds = seconds,
    on_earth = calculator(1),
    on_mercury = calculator(0.2408467),
    on_venus = calculator(0.61519726),
    on_mars =  calculator(1.8808158),
    on_jupiter = calculator(11.862615),
    on_saturn = calculator(29.447498),
    on_uranus = calculator(84.016846),
    on_neptune = calculator(164.79132)

return space_age

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?