Avatar of rabuf

rabuf's solution

to Gigasecond in the Common Lisp Track

Published at Jan 20 2020 · 0 comments
Instructions
Test suite
Solution

Given a moment, determine the moment that would be after a gigasecond has passed.

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

Setup

Check out Installing Common Lisp for instructions to get started or take a look at the guides available in the track's side bar.

Formatting

While Common Lisp doesn't care about indentation and layout of code, nor whether you use spaces or tabs, this is an important consideration for submissions to exercism.io. Excercism.io's code widget cannot handle mixing of tab and space characters well so using only spaces is recommended to make the code more readable to the human reviewers. Please review your editors settings on how to accomplish this. Below are instructions for popular editors for Common Lisp.

VIM

Use the following commands to ensure VIM uses only spaces for indentation:

:set tabstop=2
:set shiftwidth=2
:set expandtab

(or as a oneliner :set tabstop=2 shiftwidth=2 expandtab). This can be added to your ~/.vimrc file to use it all the time.

Emacs

Emacs is very well suited for editing Common Lisp and has many powerful add-on packages available. The only thing that one needs to do with a stock emacs to make it work well with exercism.io is to evaluate the following code:

(setq-default indent-tabs-mode nil)

This can be placed in your ~/.emacs (or ~/.emacs.d/init.el) in order to have it set whenever Emacs is launched.

One suggested add-on for Emacs and Common Lisp is SLIME which offers tight integration with the REPL; making iterative coding and testing very easy.

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-test.lisp

(ql:quickload "lisp-unit")
#-xlisp-test (load "gigasecond")

(defpackage #:gigasecond-test
  (:use #:cl #:lisp-unit))

(in-package #:gigasecond-test)

(define-test from-lisp-epoch
  (assert-equal '(1931 9 10 1 46 40) (gigasecond:from 1900 1 1 0 0 0)))

(define-test from-unix-epoch
  (assert-equal '(2001 9 9 1 46 40) (gigasecond:from 1970 1 1 0 0 0)))

(define-test from-20110425T120000Z
  (assert-equal '(2043 1 1 13 46 40) (gigasecond:from 2011 4 25 12 0 0)))

(define-test from-19770613T235959Z
  (assert-equal '(2009 2 20 1 46 39) (gigasecond:from 1977 6 13 23 59 59)))

(define-test from-19590719T123030Z
  (assert-equal '(1991 3 27 14 17 10) (gigasecond:from 1959 7 19 12 30 30)))

; customize this to test your birthday and find your gigasecond date:
; (define-test your-birthday
;   (assert-equal '(year2 month2 day2) (gigasecond:from year1 month1 day1)))

#-xlisp-test
(let ((*print-errors* t)
      (*print-failures* t))
  (run-tests :all :gigasecond-test))
(in-package #:cl-user)
(defpackage #:gigasecond
  (:use #:cl)
  (:export #:from))
(in-package #:gigasecond)

(defun from (year month day hour minute second)
  (multiple-value-bind (second minute hour date month year)
      (decode-universal-time (+ (expt 10 9) (encode-universal-time second minute hour day month year 0)) 0)
    (list year month date hour minute second)))

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?