Skip to content

Commit

Permalink
gigasecond: add exercise (#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom authored Oct 1, 2024
1 parent b0b878c commit c47d46f
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,14 @@
"prerequisites": [],
"difficulty": 4
},
{
"slug": "gigasecond",
"name": "Gigasecond",
"uuid": "c9ba947d-8def-4a3f-bfc2-616301f6037f",
"practices": [],
"prerequisites": [],
"difficulty": 1
},
{
"slug": "square-root",
"name": "Square Root",
Expand Down
8 changes: 8 additions & 0 deletions exercises/practice/gigasecond/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Instructions

Your task is to determine the date and time one gigasecond after a certain date.

A gigasecond is one thousand million seconds.
That is a one with nine zeros after it.

If you were born on _January 24th, 2015 at 22:00 (10:00:00pm)_, then you would be a gigasecond old on _October 2nd, 2046 at 23:46:40 (11:46:40pm)_.
24 changes: 24 additions & 0 deletions exercises/practice/gigasecond/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Introduction

The way we measure time is kind of messy.
We have 60 seconds in a minute, and 60 minutes in an hour.
This comes from ancient Babylon, where they used 60 as the basis for their number system.
We have 24 hours in a day, 7 days in a week, and how many days in a month?
Well, for days in a month it depends not only on which month it is, but also on what type of calendar is used in the country you live in.

What if, instead, we only use seconds to express time intervals?
Then we can use metric system prefixes for writing large numbers of seconds in more easily comprehensible quantities.

- A food recipe might explain that you need to let the brownies cook in the oven for two kiloseconds (that's two thousand seconds).
- Perhaps you and your family would travel to somewhere exotic for two megaseconds (that's two million seconds).
- And if you and your spouse were married for _a thousand million_ seconds, you would celebrate your one gigasecond anniversary.

~~~~exercism/note
If we ever colonize Mars or some other planet, measuring time is going to get even messier.
If someone says "year" do they mean a year on Earth or a year on Mars?
The idea for this exercise came from the science fiction novel ["A Deepness in the Sky"][vinge-novel] by author Vernor Vinge.
In it the author uses the metric system as the basis for time measurements.
[vinge-novel]: https://www.tor.com/2017/08/03/science-fiction-with-something-for-everyone-a-deepness-in-the-sky-by-vernor-vinge/
~~~~
19 changes: 19 additions & 0 deletions exercises/practice/gigasecond/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"erikschierboom"
],
"files": {
"solution": [
"gigasecond.R"
],
"test": [
"test_gigasecond.R"
],
"example": [
".meta/example.R"
]
},
"blurb": "Given a moment, determine the moment that would be after a gigasecond has passed.",
"source": "Chapter 9 in Chris Pine's online Learn to Program tutorial.",
"source_url": "https://pine.fm/LearnToProgram/?Chapter=09"
}
1 change: 1 addition & 0 deletions exercises/practice/gigasecond/.meta/example.R
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_gigasecond <- \(moment) { moment + 1e9 }
29 changes: 29 additions & 0 deletions exercises/practice/gigasecond/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[92fbe71c-ea52-4fac-bd77-be38023cacf7]
description = "date only specification of time"

[6d86dd16-6f7a-47be-9e58-bb9fb2ae1433]
description = "second test for date only specification of time"

[77eb8502-2bca-4d92-89d9-7b39ace28dd5]
description = "third test for date only specification of time"

[c9d89a7d-06f8-4e28-a305-64f1b2abc693]
description = "full time specified"

[09d4e30e-728a-4b52-9005-be44a58d9eba]
description = "full time with day roll-over"

[fcec307c-7529-49ab-b0fe-20309197618a]
description = "does not mutate the input"
include = false
3 changes: 3 additions & 0 deletions exercises/practice/gigasecond/gigasecond.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add_gigasecond <- function(moment) {

}
37 changes: 37 additions & 0 deletions exercises/practice/gigasecond/test_gigasecond.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
source("./gigasecond.R")
library(testthat)

test_that("Date only specification of time", {
expect_equal(
add_gigasecond(ISOdate(2011, 4, 25)),
ISOdate(2043, 1, 1, 13, 46, 40)
)
})

test_that("Second test for date only specification of time", {
expect_equal(
add_gigasecond(ISOdate(1977, 6, 13)),
ISOdate(2009, 2, 19, 13, 46, 40)
)
})

test_that("Third test for date only specification of time", {
expect_equal(
add_gigasecond(ISOdate(1959, 7, 19)),
ISOdate(1991, 3, 27, 13, 46, 40)
)
})

test_that("Full time specified", {
expect_equal(
add_gigasecond(ISOdate(2015, 1, 24, 22, 0, 0)),
ISOdate(2046, 10, 2, 23, 46, 40)
)
})

test_that("Full time with day roll-over", {
expect_equal(
add_gigasecond(ISOdate(2015, 1, 24, 23, 59, 59)),
ISOdate(2046, 10, 3, 1, 46, 39)
)
})

0 comments on commit c47d46f

Please sign in to comment.