Avatar of venkateshpitta

venkateshpitta's solution

to Space Age in the OCaml Track

Published at Aug 08 2019 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

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.

Getting Started

  1. Install the Exercism CLI.

  2. Install OCaml.

  3. For library documentation, follow Useful OCaml resources.

Running Tests

A Makefile is provided with a default target to compile your solution and run the tests. At the command line, type:

make

Submitting Incomplete Solutions

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

Feedback, Issues, Pull Requests

The exercism/ocaml repository on GitHub is the home for all of the Ocaml exercises.

If you have feedback about an exercise, or want to help implementing a new one, head over there and create an issue or submit a PR. We welcome new contributors!

Source

Partially inspired by Chapter 1 in Chris Pine's online Learn to Program tutorial. http://pine.fm/LearnToProgram/?Chapter=01

test.ml

open Base
open OUnit2
open Space_age

let ae ~delta:delta exp got _ctxt =
  let msg = Printf.sprintf "Expected %f got %f, difference is greater than %f"
      exp got delta in
  assert_bool msg (cmp_float ~epsilon:delta exp got)

let tests = [
  "age on Earth" >::
  ae ~delta:0.05 31.69 (age_on Earth 1000000000);
  "age on Mercury" >::
  ae ~delta:0.05 280.88 (age_on Mercury 2134835688);
  "age on Venus" >::
  ae ~delta:0.05 9.78 (age_on Venus 189839836);
  "age on Mars" >::
  ae ~delta:0.05 35.88 (age_on Mars 2129871239);
  "age on Jupiter" >::
  ae ~delta:0.05 2.41 (age_on Jupiter 901876382);
  "age on Saturn" >::
  ae ~delta:0.05 2.15 (age_on Saturn 2000000000);
  "age on Uranus" >::
  ae ~delta:0.05 0.46 (age_on Uranus 1210123456);
  "age on Neptune" >::
  ae ~delta:0.05 0.35 (age_on Neptune 1821023456);
]

let () =
  run_test_tt_main ("space-age tests" >::: tests)
type planet = Mercury | Venus | Earth | Mars
            | Jupiter | Saturn | Neptune | Uranus

let age_on p s =
  let earth_age = float_of_int s /. 31557600. in
  match p with
  | Earth   -> earth_age
  | Mercury -> earth_age /. 0.2408467
  | Venus   -> earth_age /. 0.61519726
  | Mars    -> earth_age /. 1.8808158
  | Jupiter -> earth_age /. 11.862615
  | Saturn  -> earth_age /. 29.447498
  | Uranus  -> earth_age /. 84.016846
  | Neptune -> earth_age /. 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?