From 2b1e10247b19b4b349859b0d07cd8ad265b32ab2 Mon Sep 17 00:00:00 2001 From: Danylo Negrienko Date: Tue, 2 Jul 2024 13:06:46 -0400 Subject: [PATCH] gigasecond --- elixir/gigasecond/.exercism/config.json | 34 ++++++++++ elixir/gigasecond/.exercism/metadata.json | 1 + elixir/gigasecond/.formatter.exs | 4 ++ elixir/gigasecond/.gitignore | 24 +++++++ elixir/gigasecond/HELP.md | 75 ++++++++++++++++++++++ elixir/gigasecond/README.md | 64 ++++++++++++++++++ elixir/gigasecond/lib/gigasecond.ex | 17 +++++ elixir/gigasecond/mix.exs | 28 ++++++++ elixir/gigasecond/test/gigasecond_test.exs | 30 +++++++++ elixir/gigasecond/test/test_helper.exs | 2 + 10 files changed, 279 insertions(+) create mode 100644 elixir/gigasecond/.exercism/config.json create mode 100644 elixir/gigasecond/.exercism/metadata.json create mode 100644 elixir/gigasecond/.formatter.exs create mode 100644 elixir/gigasecond/.gitignore create mode 100644 elixir/gigasecond/HELP.md create mode 100644 elixir/gigasecond/README.md create mode 100644 elixir/gigasecond/lib/gigasecond.ex create mode 100644 elixir/gigasecond/mix.exs create mode 100644 elixir/gigasecond/test/gigasecond_test.exs create mode 100644 elixir/gigasecond/test/test_helper.exs diff --git a/elixir/gigasecond/.exercism/config.json b/elixir/gigasecond/.exercism/config.json new file mode 100644 index 0000000..e48bad9 --- /dev/null +++ b/elixir/gigasecond/.exercism/config.json @@ -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" +} diff --git a/elixir/gigasecond/.exercism/metadata.json b/elixir/gigasecond/.exercism/metadata.json new file mode 100644 index 0000000..51aa3c9 --- /dev/null +++ b/elixir/gigasecond/.exercism/metadata.json @@ -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} \ No newline at end of file diff --git a/elixir/gigasecond/.formatter.exs b/elixir/gigasecond/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/elixir/gigasecond/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/elixir/gigasecond/.gitignore b/elixir/gigasecond/.gitignore new file mode 100644 index 0000000..c8fc0e7 --- /dev/null +++ b/elixir/gigasecond/.gitignore @@ -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 + diff --git a/elixir/gigasecond/HELP.md b/elixir/gigasecond/HELP.md new file mode 100644 index 0000000..100c572 --- /dev/null +++ b/elixir/gigasecond/HELP.md @@ -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/.exs:LINENUM` - runs only a single test, the test from `.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. \ No newline at end of file diff --git a/elixir/gigasecond/README.md b/elixir/gigasecond/README.md new file mode 100644 index 0000000..5b83c66 --- /dev/null +++ b/elixir/gigasecond/README.md @@ -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 \ No newline at end of file diff --git a/elixir/gigasecond/lib/gigasecond.ex b/elixir/gigasecond/lib/gigasecond.ex new file mode 100644 index 0000000..f7c11d8 --- /dev/null +++ b/elixir/gigasecond/lib/gigasecond.ex @@ -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 diff --git a/elixir/gigasecond/mix.exs b/elixir/gigasecond/mix.exs new file mode 100644 index 0000000..b9835b9 --- /dev/null +++ b/elixir/gigasecond/mix.exs @@ -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 diff --git a/elixir/gigasecond/test/gigasecond_test.exs b/elixir/gigasecond/test/gigasecond_test.exs new file mode 100644 index 0000000..e116c97 --- /dev/null +++ b/elixir/gigasecond/test/gigasecond_test.exs @@ -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 diff --git a/elixir/gigasecond/test/test_helper.exs b/elixir/gigasecond/test/test_helper.exs new file mode 100644 index 0000000..35fc5bf --- /dev/null +++ b/elixir/gigasecond/test/test_helper.exs @@ -0,0 +1,2 @@ +ExUnit.start() +ExUnit.configure(exclude: :pending, trace: true)