Given an age in seconds, calculate how old someone would be on:
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.
To run the tests, run the command busted
from within the exercise directory.
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
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)
end)
it('age in Earth years', function()
local age = SpaceAge:new(1000000000)
assert.are.equal(31.69, age.on_earth())
end)
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())
end)
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())
end)
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())
end)
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())
end)
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())
end)
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())
end)
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())
end)
end)
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
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)
}
end
return space_age
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.
Level up your programming skills with 3,449 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments