Avatar of JaeHyoLee

JaeHyoLee's solution

to Gigasecond in the Lua Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Calculate the moment when someone has lived for 10^9 seconds.

A gigasecond is 10^9 (1,000,000,000) seconds.

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.

Source

Chapter 9 in Chris Pine's online Learn to Program tutorial. http://pine.fm/LearnToProgram/?Chapter=09

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

gigasecond_spec.lua

local gigasecond = require('gigasecond')

describe('gigasecond', function()
  it('test 1', function()
    local actual = gigasecond.anniversary(os.time({ year = 2011, month = 3, day = 25, hour = 0, min = 0, sec = 0 }))
    local expectedDate = os.date('%x', os.time({ year = 2042, month = 12, day = 1, hour = 0, min = 0, sec = 0 }))
    assert.are.equals(expectedDate, actual)
  end)

  it('test 2', function()
    local actual = gigasecond.anniversary(os.time({ year = 1977, month = 5, day = 13, hour = 0, min = 0, sec = 0 }))
    local expectedDate = os.date('%x', os.time({ year = 2009, month = 1, day = 19 }))
    assert.are.equals(expectedDate, actual)
  end)

  it('test 3', function()
    local actual = gigasecond.anniversary(os.time({ year = 1959, month = 7, day = 19 }))
    local expectedDate = os.date('%x', os.time({ year = 1991, month = 3, day = 27 }))
    assert.are.equals(expectedDate, actual)
  end)

  it('test 4', function()
    local actual = gigasecond.anniversary(os.time({ year = 1993, month = 8, day = 17 }))
    local expectedDate = os.date('%x', os.time({ year = 2025, month = 4, day = 25 }))
    assert.are.equals(expectedDate, actual)
  end)
end)
local function anniversary(begin)
  local giga_seconds = 1000000000
  return os.date('%x', begin + giga_seconds)
end

return { anniversary = anniversary}

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?