gigasecond

This commit is contained in:
Danil Negrienko 2024-07-02 13:06:46 -04:00
parent 81b389528e
commit 2b1e10247b
10 changed files with 279 additions and 0 deletions

View File

@ -0,0 +1,34 @@
{
"authors": [
"rubysolo"
],
"contributors": [
"andrewsardone",
"angelikatyborska",
"Cohen-Carlisle",
"dalexj",
"devonestes",
"jinyeow",
"lpil",
"neenjaw",
"parkerl",
"petehuang",
"sotojuan",
"Teapane",
"waiting-for-dev"
],
"files": {
"solution": [
"lib/gigasecond.ex"
],
"test": [
"test/gigasecond_test.exs"
],
"example": [
".meta/example.ex"
]
},
"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"
}

View File

@ -0,0 +1 @@
{"track":"elixir","exercise":"gigasecond","id":"5a877667e1af4bd5aa468ba8cdfb94aa","url":"https://exercism.org/tracks/elixir/exercises/gigasecond","handle":"negrienko","is_requester":true,"auto_approve":false}

View File

@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]

24
elixir/gigasecond/.gitignore vendored Normal file
View File

@ -0,0 +1,24 @@
# The directory Mix will write compiled artifacts to.
/_build/
# If you run "mix test --cover", coverage assets end up here.
/cover/
# The directory Mix downloads your dependencies sources to.
/deps/
# Where third-party dependencies like ExDoc output generated docs.
/doc/
# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch
# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump
# Also ignore archive artifacts (built via "mix archive.build").
*.ez
# Ignore package tarball (built via "mix hex.build").
gigasecond-*.tar

75
elixir/gigasecond/HELP.md Normal file
View File

@ -0,0 +1,75 @@
# Help
## Running the tests
From the terminal, change to the base directory of the exercise then execute the tests with:
```bash
$ mix test
```
This will execute the test file found in the `test` subfolder -- a file ending in `_test.exs`
Documentation:
* [`mix test` - Elixir's test execution tool](https://hexdocs.pm/mix/Mix.Tasks.Test.html)
* [`ExUnit` - Elixir's unit test library](https://hexdocs.pm/ex_unit/ExUnit.html)
## Pending tests
In test suites of practice exercises, all but the first test have been tagged to be skipped.
Once you get a test passing, you can unskip the next one by commenting out the relevant `@tag :pending` with a `#` symbol.
For example:
```elixir
# @tag :pending
test "shouting" do
assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end
```
If you wish to run all tests at once, you can include all skipped test by using the `--include` flag on the `mix test` command:
```bash
$ mix test --include pending
```
Or, you can enable all the tests by commenting out the `ExUnit.configure` line in the file `test/test_helper.exs`.
```elixir
# ExUnit.configure(exclude: :pending, trace: true)
```
## Useful `mix test` options
* `test/<FILE>.exs:LINENUM` - runs only a single test, the test from `<FILE>.exs` whose definition is on line `LINENUM`
* `--failed` - runs only tests that failed the last time they ran
* `--max-failures` - the suite stops evaluating tests when this number of test failures
is reached
* `--seed 0` - disables randomization so the tests in a single file will always be ran
in the same order they were defined in
## Submitting your solution
You can submit your solution using the `exercism submit lib/gigasecond.ex` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [Elixir track's documentation](https://exercism.org/docs/tracks/elixir)
- The [Elixir track's programming category on the forum](https://forum.exercism.org/c/programming/elixir)
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
If you're stuck on something, it may help to look at some of the [available resources](https://exercism.org/docs/tracks/elixir/resources) out there where answers might be found.

View File

@ -0,0 +1,64 @@
# Gigasecond
Welcome to Gigasecond on Exercism's Elixir Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## 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/
~~~~
## 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)_.
## Source
### Created by
- @rubysolo
### Contributed to by
- @andrewsardone
- @angelikatyborska
- @Cohen-Carlisle
- @dalexj
- @devonestes
- @jinyeow
- @lpil
- @neenjaw
- @parkerl
- @petehuang
- @sotojuan
- @Teapane
- @waiting-for-dev
### Based on
Chapter 9 in Chris Pine's online Learn to Program tutorial. - https://pine.fm/LearnToProgram/?Chapter=09

View File

@ -0,0 +1,17 @@
defmodule Gigasecond do
@gigasecond 1_000_000_000
@doc """
Calculate a date one billion seconds after an input date.
"""
@spec from({{pos_integer, pos_integer, pos_integer}, {pos_integer, pos_integer, pos_integer}}) ::
{{pos_integer, pos_integer, pos_integer}, {pos_integer, pos_integer, pos_integer}}
def from({{_year, _month, _day}, {_hours, _minutes, _seconds}} = erl_date_time) do
{:ok, from} =
NaiveDateTime.from_erl(erl_date_time)
from
|> NaiveDateTime.add(@gigasecond, :second)
|> NaiveDateTime.to_erl()
end
end

28
elixir/gigasecond/mix.exs Normal file
View File

@ -0,0 +1,28 @@
defmodule Gigasecond.MixProject do
use Mix.Project
def project do
[
app: :gigasecond,
version: "0.1.0",
# elixir: "~> 1.8",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end
# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger]
]
end
# Run "mix help deps" to learn about dependencies.
defp deps do
[
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
]
end
end

View File

@ -0,0 +1,30 @@
defmodule GigasecondTest do
use ExUnit.Case
test "from 2011-04-25 00:00:00" do
assert Gigasecond.from({{2011, 4, 25}, {0, 0, 0}}) == {{2043, 1, 1}, {1, 46, 40}}
end
test "from 1977-06-13 00:00:00" do
assert Gigasecond.from({{1977, 6, 13}, {0, 0, 0}}) == {{2009, 2, 19}, {1, 46, 40}}
end
test "from 1959-19-07 00:00:00" do
assert Gigasecond.from({{1959, 7, 19}, {0, 0, 0}}) == {{1991, 3, 27}, {1, 46, 40}}
end
test "from 2015-01-24 22:00:00" do
assert Gigasecond.from({{2015, 1, 24}, {22, 0, 0}}) == {{2046, 10, 2}, {23, 46, 40}}
end
test "from 2015-01-24 23:59:59" do
assert Gigasecond.from({{2015, 1, 24}, {23, 59, 59}}) == {{2046, 10, 3}, {1, 46, 39}}
end
@tag :pending
test "yourself" do
# customize these values for yourself
# your_birthday = {{year1, month1, day1}, {0, 0, 0}}
# assert Gigasecond.from(your_birthday) == {{year2, month2, day2}, {hours, minutes, seconds}}
end
end

View File

@ -0,0 +1,2 @@
ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)