ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# katrinleinweber's solution

## to Space Age in the R Track

Published at Jul 13 2018 · 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.

## Installation

See this guide for instructions on how to setup your local R environment.

## How to implement your solution

In each problem folder, there is a file named `<exercise_name>.R` containing a function that returns a `NULL` value. Place your implementation inside the body of the function.

## How to run tests

Inside of RStudio, simply execute the `test_<exercise_name>.R` script. This can be conveniently done with testthat's `auto_test` function. Because exercism code and tests are in the same folder, use this same path for both `code_path` and `test_path` parameters. On the command-line, you can also run `Rscript test_<exercise_name>.R`.

## 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.

### test_space-age.R

``````source("./space-age.R")
library(testthat)

context("space age")

test_that("Age on Earth", {
seconds <- 1000000000
expect_equal(space_age(seconds, "earth"), 31.69)
})

test_that("Age on Mercury", {
seconds <- 2134835688
expect_equal(space_age(seconds, "mercury"), 280.88)
})

test_that("Age on Venus", {
seconds <- 189839836
expect_equal(space_age(seconds, "venus"), 9.78)
})

test_that("Age on Mars", {
seconds <- 2329871239
expect_equal(space_age(seconds, "mars"), 39.25)
})

test_that("Age on Jupiter", {
seconds <- 901876382
expect_equal(space_age(seconds, "jupiter"), 2.41)
})

test_that("Age on Saturn", {
seconds <- 3000000000
expect_equal(space_age(seconds, "saturn"), 3.23)
})

test_that("Age on Uranus", {
seconds <- 3210123456
expect_equal(space_age(seconds, "uranus"), 1.21)
})

test_that("Age on Neptune", {
seconds <- 8210123456
expect_equal(space_age(seconds, "neptune"), 1.58)
})

message("All tests passed for exercise: space-age")``````
``````space_age <- function(seconds, planet) {

# switch provided planet name with its relative orbital period
convert <- function(planet) {
switch(planet,
"mercury" = 0.2408467,
"venus"   = 0.61519726,
"earth"   = 1,
"mars"    = 1.8808158,
"jupiter" = 11.862615,
"saturn"  = 29.447498,
"uranus"  = 84.016846,
"neptune" = 164.79132)
}

# divide given age by product of sec_per_Earth_year and relative orbital period
round(seconds / (31557600 * convert(planet)), 2)
}``````