From f3067bce0fe674d4b24eba2cdc799a7c8635613a Mon Sep 17 00:00:00 2001 From: Danylo Negrienko Date: Sun, 17 Dec 2023 18:32:39 -0500 Subject: [PATCH] leap --- elixir/leap/.exercism/config.json | 35 ++++++++++++++ elixir/leap/.exercism/metadata.json | 1 + elixir/leap/.formatter.exs | 4 ++ elixir/leap/.gitignore | 24 +++++++++ elixir/leap/HELP.md | 75 +++++++++++++++++++++++++++++ elixir/leap/README.md | 54 +++++++++++++++++++++ elixir/leap/lib/year.ex | 16 ++++++ elixir/leap/mix.exs | 28 +++++++++++ elixir/leap/test/test_helper.exs | 2 + elixir/leap/test/year_test.exs | 48 ++++++++++++++++++ 10 files changed, 287 insertions(+) create mode 100644 elixir/leap/.exercism/config.json create mode 100644 elixir/leap/.exercism/metadata.json create mode 100644 elixir/leap/.formatter.exs create mode 100644 elixir/leap/.gitignore create mode 100644 elixir/leap/HELP.md create mode 100644 elixir/leap/README.md create mode 100644 elixir/leap/lib/year.ex create mode 100644 elixir/leap/mix.exs create mode 100644 elixir/leap/test/test_helper.exs create mode 100644 elixir/leap/test/year_test.exs diff --git a/elixir/leap/.exercism/config.json b/elixir/leap/.exercism/config.json new file mode 100644 index 0000000..6fd2402 --- /dev/null +++ b/elixir/leap/.exercism/config.json @@ -0,0 +1,35 @@ +{ + "authors": [ + "rubysolo" + ], + "contributors": [ + "angelikatyborska", + "Cohen-Carlisle", + "dalexj", + "devonestes", + "glennular", + "jinyeow", + "korbin", + "kytrinyx", + "lpil", + "neenjaw", + "parkerl", + "sotojuan", + "Teapane", + "waiting-for-dev" + ], + "files": { + "solution": [ + "lib/year.ex" + ], + "test": [ + "test/year_test.exs" + ], + "example": [ + ".meta/example.ex" + ] + }, + "blurb": "Given a year, report if it is a leap year.", + "source": "CodeRanch Cattle Drive, Assignment 3", + "source_url": "https://coderanch.com/t/718816/Leap" +} diff --git a/elixir/leap/.exercism/metadata.json b/elixir/leap/.exercism/metadata.json new file mode 100644 index 0000000..50ef635 --- /dev/null +++ b/elixir/leap/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"elixir","exercise":"leap","id":"8bd50f0b5d7c4308bca361b49e63d755","url":"https://exercism.org/tracks/elixir/exercises/leap","handle":"negrienko","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/elixir/leap/.formatter.exs b/elixir/leap/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/elixir/leap/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/elixir/leap/.gitignore b/elixir/leap/.gitignore new file mode 100644 index 0000000..baae2d4 --- /dev/null +++ b/elixir/leap/.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"). +leap-*.tar + diff --git a/elixir/leap/HELP.md b/elixir/leap/HELP.md new file mode 100644 index 0000000..c205ce6 --- /dev/null +++ b/elixir/leap/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/year.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/leap/README.md b/elixir/leap/README.md new file mode 100644 index 0000000..5dd5b0c --- /dev/null +++ b/elixir/leap/README.md @@ -0,0 +1,54 @@ +# Leap + +Welcome to Leap on Exercism's Elixir Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Instructions + +Given a year, report if it is a leap year. + +The tricky thing here is that a leap year in the Gregorian calendar occurs: + +```text +on every year that is evenly divisible by 4 + except every year that is evenly divisible by 100 + unless the year is also evenly divisible by 400 +``` + +For example, 1997 is not a leap year, but 1996 is. +1900 is not a leap year, but 2000 is. + +## Notes + +Though our exercise adopts some very simple rules, there is more to learn! + +For a delightful, four minute explanation of the whole leap year phenomenon, go watch [this youtube video][video]. + +[video]: https://www.youtube.com/watch?v=xX96xng7sAE + +## Source + +### Created by + +- @rubysolo + +### Contributed to by + +- @angelikatyborska +- @Cohen-Carlisle +- @dalexj +- @devonestes +- @glennular +- @jinyeow +- @korbin +- @kytrinyx +- @lpil +- @neenjaw +- @parkerl +- @sotojuan +- @Teapane +- @waiting-for-dev + +### Based on + +CodeRanch Cattle Drive, Assignment 3 - https://coderanch.com/t/718816/Leap \ No newline at end of file diff --git a/elixir/leap/lib/year.ex b/elixir/leap/lib/year.ex new file mode 100644 index 0000000..627fdea --- /dev/null +++ b/elixir/leap/lib/year.ex @@ -0,0 +1,16 @@ +defmodule Year do + @doc """ + Returns whether 'year' is a leap year. + + A leap year occurs: + + on every year that is evenly divisible by 4 + except every year that is evenly divisible by 100 + unless the year is also evenly divisible by 400 + """ + @spec leap_year?(non_neg_integer) :: boolean + def leap_year?(year) when rem(year, 400) == 0, do: true + def leap_year?(year) when rem(year, 100) == 0, do: false + def leap_year?(year) when rem(year, 4) == 0, do: true + def leap_year?(_year), do: false +end diff --git a/elixir/leap/mix.exs b/elixir/leap/mix.exs new file mode 100644 index 0000000..a74f3c9 --- /dev/null +++ b/elixir/leap/mix.exs @@ -0,0 +1,28 @@ +defmodule Year.MixProject do + use Mix.Project + + def project do + [ + app: :year, + 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/leap/test/test_helper.exs b/elixir/leap/test/test_helper.exs new file mode 100644 index 0000000..35fc5bf --- /dev/null +++ b/elixir/leap/test/test_helper.exs @@ -0,0 +1,2 @@ +ExUnit.start() +ExUnit.configure(exclude: :pending, trace: true) diff --git a/elixir/leap/test/year_test.exs b/elixir/leap/test/year_test.exs new file mode 100644 index 0000000..d364ff3 --- /dev/null +++ b/elixir/leap/test/year_test.exs @@ -0,0 +1,48 @@ +defmodule YearTest do + use ExUnit.Case + + # @tag :pending + test "year not divisible by 4 is common year" do + refute Year.leap_year?(2015) + end + + # @tag :pending + test "year divisible by 2, not divisible by 4 is common year" do + refute Year.leap_year?(1970) + end + + # @tag :pending + test "year divisible by 4, not divisible by 100 is leap year" do + assert Year.leap_year?(1996) + end + + # @tag :pending + test "year divisible by 4 and 5 is still a leap year" do + assert Year.leap_year?(1960) + end + + @tag :pending + test "year divisible by 100, not divisible by 400 is common year" do + refute Year.leap_year?(2100) + end + + # @tag :pending + test "year divisible by 100 but not by 3 is still not a leap year" do + refute Year.leap_year?(1900) + end + + # @tag :pending + test "year divisible by 400 is leap year" do + assert Year.leap_year?(2000) + end + + # @tag :pending + test "year divisible by 400 but not by 125 is still a leap year" do + assert Year.leap_year?(2400) + end + + # @tag :pending + test "year divisible by 200, not divisible by 400 in common year" do + refute Year.leap_year?(1800) + end +end