Avatar of cloudsftp

cloudsftp's solution

to Space Age in the Common Lisp Track

Published at Mar 03 2020 · 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.

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

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.

space-age-test.lisp

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

(defpackage #:space-age-test
  (:use #:common-lisp #:lisp-unit))

(in-package #:space-age-test)

(defun rounds-to (expected actual)
  (flet ((to-2-places (n) (/ (fround (* 100 n)))))
    (assert-equal (to-2-places expected)
			(to-2-places actual))))

(define-test age-in-earth-years
  (rounds-to 31.69 (space-age:on-earth 1000000001)))

(define-test age-in-mercury-years
  (let ((seconds 2134835688))
    (rounds-to 67.65 (space-age:on-earth seconds))
    (rounds-to 280.88 (space-age:on-mercury seconds))))

(define-test age-in-venus-years
  (let ((seconds 189839836))
    (rounds-to 6.02 (space-age:on-earth seconds))
    (rounds-to 9.78 (space-age:on-venus seconds))))

(define-test age-on-mars
  (let ((seconds 2329871239))
    (rounds-to 73.83 (space-age:on-earth seconds))
    (rounds-to 39.25 (space-age:on-mars seconds))))

(define-test age-on-jupiter
  (let ((seconds 901876382))
    (rounds-to 28.58 (space-age:on-earth seconds))
    (rounds-to 2.41 (space-age:on-jupiter seconds))))

(define-test age-on-saturn
  (let ((seconds 3000000000))
    (rounds-to 95.06 (space-age:on-earth seconds))
    (rounds-to 3.23 (space-age:on-saturn seconds))))

(define-test age-on-uranus
  (let ((seconds 3210123456))
    (rounds-to 101.72 (space-age:on-earth seconds))
    (rounds-to 1.21 (space-age:on-uranus seconds))))

(define-test age-on-neptune
  (let ((seconds 8210123456))
    (rounds-to 260.16 (space-age:on-earth seconds))
    (rounds-to 1.58 (space-age:on-neptune seconds))))

#-xlisp-test
(let ((*print-errors* t)
      (*print-failures* t))
  (run-tests :all :space-age-test))
(in-package #:cl-user)
(defpackage #:space-age
  (:use #:common-lisp)
  (:export
   :on-earth
   :on-mercury
   :on-venus
   :on-mars
   :on-jupiter
   :on-saturn
   :on-uranus
   :on-neptune))

(in-package #:space-age)

(defun on-earth (seconds)
  (/ seconds 31557600))

(defun on-mercury (seconds)
  (/ (on-earth seconds) 0.2408467))

(defun on-venus (seconds)
  (/ (on-earth seconds) 0.61519726))

(defun on-mars (seconds)
  (/ (on-earth seconds) 1.8808158))

(defun on-jupiter (seconds)
  (/ (on-earth seconds) 11.862615))

(defun on-saturn (seconds)
  (/ (on-earth seconds) 29.447498))

(defun on-uranus (seconds)
  (/ (on-earth seconds) 84.016846))

(defun on-neptune (seconds)
  (/ (on-earth seconds) 164.79132))

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?