From 2465a18235214775d49190d983530a6a0e951439 Mon Sep 17 00:00:00 2001 From: Danylo Negrienko Date: Tue, 19 Dec 2023 21:32:19 -0500 Subject: [PATCH] library-fees --- elixir/library-fees/.exercism/config.json | 21 +++ elixir/library-fees/.exercism/metadata.json | 1 + elixir/library-fees/.formatter.exs | 4 + elixir/library-fees/.gitignore | 24 +++ elixir/library-fees/HELP.md | 75 ++++++++ elixir/library-fees/HINTS.md | 47 +++++ elixir/library-fees/README.md | 124 +++++++++++++ elixir/library-fees/lib/library_fees.ex | 55 ++++++ elixir/library-fees/mix.exs | 28 +++ .../library-fees/test/library_fees_test.exs | 171 ++++++++++++++++++ elixir/library-fees/test/test_helper.exs | 2 + 11 files changed, 552 insertions(+) create mode 100644 elixir/library-fees/.exercism/config.json create mode 100644 elixir/library-fees/.exercism/metadata.json create mode 100644 elixir/library-fees/.formatter.exs create mode 100644 elixir/library-fees/.gitignore create mode 100644 elixir/library-fees/HELP.md create mode 100644 elixir/library-fees/HINTS.md create mode 100644 elixir/library-fees/README.md create mode 100644 elixir/library-fees/lib/library_fees.ex create mode 100644 elixir/library-fees/mix.exs create mode 100644 elixir/library-fees/test/library_fees_test.exs create mode 100644 elixir/library-fees/test/test_helper.exs diff --git a/elixir/library-fees/.exercism/config.json b/elixir/library-fees/.exercism/config.json new file mode 100644 index 0000000..060a2ec --- /dev/null +++ b/elixir/library-fees/.exercism/config.json @@ -0,0 +1,21 @@ +{ + "authors": [ + "angelikatyborska" + ], + "contributors": [ + "neenjaw" + ], + "files": { + "solution": [ + "lib/library_fees.ex" + ], + "test": [ + "test/library_fees_test.exs" + ], + "exemplar": [ + ".meta/exemplar.ex" + ] + }, + "language_versions": ">=1.10", + "blurb": "Learn about dates and time by calculating late fees for the local library." +} diff --git a/elixir/library-fees/.exercism/metadata.json b/elixir/library-fees/.exercism/metadata.json new file mode 100644 index 0000000..3651f97 --- /dev/null +++ b/elixir/library-fees/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"elixir","exercise":"library-fees","id":"ca5ec29bebbb49669bd4607297760349","url":"https://exercism.org/tracks/elixir/exercises/library-fees","handle":"negrienko","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/elixir/library-fees/.formatter.exs b/elixir/library-fees/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/elixir/library-fees/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/elixir/library-fees/.gitignore b/elixir/library-fees/.gitignore new file mode 100644 index 0000000..4abee35 --- /dev/null +++ b/elixir/library-fees/.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"). +match_binary-*.tar + diff --git a/elixir/library-fees/HELP.md b/elixir/library-fees/HELP.md new file mode 100644 index 0000000..e870165 --- /dev/null +++ b/elixir/library-fees/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/library_fees.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/library-fees/HINTS.md b/elixir/library-fees/HINTS.md new file mode 100644 index 0000000..018b039 --- /dev/null +++ b/elixir/library-fees/HINTS.md @@ -0,0 +1,47 @@ +# Hints + +## General + +- Review the functions available in the [`NaiveDateTime` module][naive-date-time], the [`Date` module][date], and the [`Time` module][time]. + +## 1. Parse the stored datetimes + +- There is a [built-in function][naive-date-time-from-iso8601] that parses an ISO8601 datetime string and returns a `NaiveDateTime` struct. + +## 2. Determine if a book was checked out before noon + +- You can define a `Time` literal using the [`~T` sigil][time-sigil]. +- There is a [built-in function][naive-date-time-to-time] that changes a `NaiveDateTime` struct to a `Time` struct. +- There is a [built-in function][time-compare] that checks which one of two `Time` structs is bigger. + +## 3. Calculate the return date + +- A day has `24 * 60 * 60` seconds. +- There is a [built-in function][naive-date-time-add] that adds a given number of seconds to a `NaiveDateTime` struct. +- There is a [built-in function][naive-date-time-to-date] that changes a `NaiveDateTime` struct to a `Date` struct. + +## 4. Determine how late the return of the book was + +- There is a [built-in function][naive-date-time-to-date] that changes a `NaiveDateTime` struct to a `Date` struct. +- There is a [built-in function][date-diff] that calculates the difference in days between two `Date` structs. + +## 5. Determine if the book was returned on a Monday + +- There is a [built-in function][naive-date-time-to-date] that changes a `NaiveDateTime` struct to a `Date` struct. +- There is a [built-in function][date-day-of-week] that returns the day of week for a given `Date` struct. + +## 6. Calculate the late fee + +- Combine together all of the functions that you defined in previous steps. + +[naive-date-time]: https://hexdocs.pm/elixir/NaiveDateTime.html +[time]: https://hexdocs.pm/elixir/Time.html +[date]: https://hexdocs.pm/elixir/Date.html +[naive-date-time-from-iso8601]: https://hexdocs.pm/elixir/NaiveDateTime.html#from_iso8601!/2 +[naive-date-time-to-time]: https://hexdocs.pm/elixir/NaiveDateTime.html#to_time/1 +[naive-date-time-to-date]: https://hexdocs.pm/elixir/NaiveDateTime.html#to_date/1 +[naive-date-time-add]: https://hexdocs.pm/elixir/NaiveDateTime.html#add/3 +[time-sigil]: https://hexdocs.pm/elixir/Kernel.html#sigil_T/2 +[time-compare]: https://hexdocs.pm/elixir/Time.html#compare/2 +[date-diff]: https://hexdocs.pm/elixir/Date.html#diff/2 +[date-day-of-week]: https://hexdocs.pm/elixir/Date.html#day_of_week/2 \ No newline at end of file diff --git a/elixir/library-fees/README.md b/elixir/library-fees/README.md new file mode 100644 index 0000000..6eeb118 --- /dev/null +++ b/elixir/library-fees/README.md @@ -0,0 +1,124 @@ +# Library Fees + +Welcome to Library Fees on Exercism's Elixir Track. +If you need help running the tests or submitting your code, check out `HELP.md`. +If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :) + +## Introduction + +## Dates and Time + +Elixir's standard library offers 4 different modules for working with dates and time, each with its own struct. + +- The `Date` module. A `Date` struct can be created with the `~D` sigil. + + ```elixir + ~D[2021-01-01] + ``` + +- The `Time` module. A `Time` struct can be created with the `~T` sigil. + + ```elixir + ~T[12:00:00] + ``` + +- The `NaiveDateTime` module for datetimes without a timezone. A `NaiveDateTime` struct can be created with the `~N` sigil. + + ```elixir + ~N[2021-01-01 12:00:00] + ``` + +- The `DateTime` module for datetimes with a timezone. Using this module for timezones other than UTC requires an external dependency, a timezone database. + +### Comparisons + +To compare dates or times to one another, look for a `compare` or `diff` function in the corresponding module. Comparison operators such as `==`, `>`, and `<` _seem_ to work, but they don't do a correct semantic comparison for those structs. + +## Instructions + +Your librarian friend has asked you to extend her library software to automatically calculate late fees. +Her current system stores the exact date and time of a book checkout as an [ISO8601](https://en.wikipedia.org/wiki/ISO_8601) datetime string. +She runs a local library in a small town in Ghana, which uses the GMT timezone (UTC +0), doesn't use daylight saving time, and doesn't need to worry about other timezones. + +## 1. Parse the stored datetimes + +Implement the `LibraryFees.datetime_from_string/1` function. It should take an ISO8601 datetime string as an argument, and return a `NaiveDateTime` struct. + +```elixir +LibraryFees.datetime_from_string("2021-01-01T13:30:45Z") +# => ~N[2021-01-01 13:30:45] +``` + +## 2. Determine if a book was checked out before noon + +If a book was checked out before noon, the reader has 28 days to return it. If it was checked out at or after noon, it's 29 days. + +Implement the `LibraryFees.before_noon?/1` function. It should take a `NaiveDateTime` struct and return a boolean. + +```elixir +LibraryFees.before_noon?(~N[2021-01-12 08:23:03]) +# => true +``` + +## 3. Calculate the return date + +Based on the checkout datetime, calculate the return date. + +Implement the `LibraryFees.return_date/1` function. It should take a `NaiveDateTime` struct and return a `Date` struct, either 28 or 29 days later. + +```elixir +LibraryFees.return_date(~N[2020-11-28 15:55:33]) +# => ~D[2020-12-27] +``` + +## 4. Determine how late the return of the book was + +The library has a flat rate for late returns. To be able to calculate the fee, we need to know how many days after the return date the book was actually returned. + +Implement the `LibraryFees.days_late/2` function. It should take a `Date` struct - the planned return date, and a `NaiveDateTime` struct - the actual return datetime. + +If the actual return date is on an earlier or the same day as the planned return datetime, the function should return 0. Otherwise, the function should return the difference between those two dates in days. + +The library tracks both the date and time of the actual return of the book for statistical purposes, but doesn't use the time when calculating late fees. + +```elixir +LibraryFees.days_late(~D[2020-12-27], ~N[2021-01-03 09:23:36]) +# => 7 +``` + +## 5. Determine if the book was returned on a Monday + +The library has a special offer for returning books on Mondays. + +Implement the `LibraryFees.monday?/1` function. It should take a `NaiveDateTime` struct and return a boolean. + +```elixir +LibraryFees.monday?(~N[2021-01-03 13:30:45Z]) +# => false +``` + +## 6. Calculate the late fee + +Implement the `LibraryFees.calculate_late_fee/3` function. It should take three arguments - two ISO8601 datetime strings, checkout datetime and actual return datetime, and the late fee for one day. It should return the total late fee according to how late the actual return of the book was. + +Include the special Monday offer. If you return the book on Monday, your late fee is 50% off, rounded down. + +```elixir +# Sunday, 7 days late +LibraryFees.calculate_late_fee("2020-11-28T15:55:33Z", "2021-01-03T13:30:45Z", 100) +# => 700 + +# one day later, Monday, 8 days late +LibraryFees.calculate_late_fee("2020-11-28T15:55:33Z", "2021-01-04T09:02:11Z", 100) +# => 400 +``` + +## Source + +### Created by + +- @angelikatyborska + +### Contributed to by + +- @neenjaw \ No newline at end of file diff --git a/elixir/library-fees/lib/library_fees.ex b/elixir/library-fees/lib/library_fees.ex new file mode 100644 index 0000000..b3fa2a9 --- /dev/null +++ b/elixir/library-fees/lib/library_fees.ex @@ -0,0 +1,55 @@ +defmodule LibraryFees do + def datetime_from_string(string) do + case NaiveDateTime.from_iso8601(string) do + {:ok, naive_date_time} -> naive_date_time + _ -> :error + end + end + + def before_noon?(datetime) do + datetime.hour < 12 + end + + defp days_to_return(checkout_datetime) do + if(before_noon?(checkout_datetime), do: 28, else: 29) + end + + def return_date(checkout_datetime) do + checkout_datetime + |> NaiveDateTime.add(days_to_return(checkout_datetime), :day) + |> NaiveDateTime.to_date() + end + + def days_late(planned_return_date, actual_return_datetime) do + actual_return_datetime + |> NaiveDateTime.to_date() + |> Date.diff(planned_return_date) + |> case do + late_in_days when late_in_days > 0 -> late_in_days + _late_in_days -> 0 + end + end + + def monday?(datetime) do + datetime + |> NaiveDateTime.to_date() + |> Date.day_of_week(:monday) + |> case do + 1 -> true + _ -> false + end + end + + def calculate_late_fee(checkout, return, rate) do + checkout_date_time = datetime_from_string(checkout) + actual_return_date_time = datetime_from_string(return) + planed_return_date_time = return_date(checkout_date_time) + + fee = days_late(planed_return_date_time, actual_return_date_time) * rate + + case monday?(actual_return_date_time) do + true -> floor(fee * 0.5) + _ -> fee + end + end +end diff --git a/elixir/library-fees/mix.exs b/elixir/library-fees/mix.exs new file mode 100644 index 0000000..2c859d3 --- /dev/null +++ b/elixir/library-fees/mix.exs @@ -0,0 +1,28 @@ +defmodule LibraryFees.MixProject do + use Mix.Project + + def project do + [ + app: :library_fees, + version: "0.1.0", + # elixir: "~> 1.10", + 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/library-fees/test/library_fees_test.exs b/elixir/library-fees/test/library_fees_test.exs new file mode 100644 index 0000000..b752777 --- /dev/null +++ b/elixir/library-fees/test/library_fees_test.exs @@ -0,0 +1,171 @@ +defmodule LibraryFeesTest do + use ExUnit.Case + + describe "datetime_from_string/1" do + @tag task_id: 1 + test "returns NaiveDateTime" do + result = LibraryFees.datetime_from_string("2021-01-01T12:00:00Z") + assert result.__struct__ == NaiveDateTime + end + + @tag task_id: 1 + test "parses an ISO8601 string" do + result = LibraryFees.datetime_from_string("2019-12-24T13:15:45Z") + assert result == ~N[2019-12-24 13:15:45Z] + end + end + + describe "before_noon?/1" do + @tag task_id: 2 + test "returns true if the given NaiveDateTime is before 12:00" do + assert LibraryFees.before_noon?(~N[2020-06-06 11:59:59Z]) == true + end + + @tag task_id: 2 + test "returns false if the given NaiveDateTime is after 12:00" do + assert LibraryFees.before_noon?(~N[2021-01-03 12:01:01Z]) == false + end + + @tag task_id: 2 + test "returns false if the given NaiveDateTime is exactly at 12:00" do + assert LibraryFees.before_noon?(~N[2018-11-17 12:00:00Z]) == false + end + end + + describe "return_date/1" do + @tag task_id: 3 + test "adds 28 days if the given NaiveDateTime is before 12:00" do + result = LibraryFees.return_date(~N[2020-02-14 11:59:59Z]) + assert result == ~D[2020-03-13] + end + + @tag task_id: 3 + test "adds 29 days if the given NaiveDateTime is after 12:00" do + result = LibraryFees.return_date(~N[2021-01-03 12:01:01Z]) + assert result == ~D[2021-02-01] + end + + @tag task_id: 3 + test "adds 29 days if the given NaiveDateTime is exactly at 12:00" do + result = LibraryFees.return_date(~N[2018-12-01 12:00:00Z]) + assert result == ~D[2018-12-30] + end + end + + describe "days_late/2" do + @tag task_id: 4 + test "returns 0 when identical datetimes" do + result = LibraryFees.days_late(~D[2021-02-01], ~N[2021-02-01 12:00:00Z]) + assert result == 0 + end + + @tag task_id: 4 + test "returns 0 when identical dates, but different times" do + result = LibraryFees.days_late(~D[2019-03-11], ~N[2019-03-11 12:00:00Z]) + assert result == 0 + end + + @tag task_id: 4 + test "returns 0 when planned return date is later than actual return date" do + result = LibraryFees.days_late(~D[2020-12-03], ~N[2020-11-29 16:00:00Z]) + assert result == 0 + end + + @tag task_id: 4 + test "returns date difference in numbers of days when planned return date is earlier than actual return date" do + result = LibraryFees.days_late(~D[2020-06-12], ~N[2020-06-21 16:00:00Z]) + assert result == 9 + end + + @tag task_id: 4 + test "a new day starts at midnight" do + result = LibraryFees.days_late(~D[2020-06-12], ~N[2020-06-12 23:59:59Z]) + assert result == 0 + + result = LibraryFees.days_late(~D[2020-06-12], ~N[2020-06-13 00:00:00Z]) + assert result == 1 + end + end + + describe "monday?" do + @tag task_id: 5 + test "2021-02-01 was a Monday" do + assert LibraryFees.monday?(~N[2021-02-01 14:01:00Z]) == true + end + + @tag task_id: 5 + test "2020-03-16 was a Monday" do + assert LibraryFees.monday?(~N[2020-03-16 09:23:52Z]) == true + end + + @tag task_id: 5 + test "2020-04-22 was a Monday" do + assert LibraryFees.monday?(~N[2019-04-22 15:44:03Z]) == true + end + + @tag task_id: 5 + test "2021-02-02 was a Tuesday" do + assert LibraryFees.monday?(~N[2021-02-02 15:07:00Z]) == false + end + + @tag task_id: 5 + test "2020-03-14 was a Friday" do + assert LibraryFees.monday?(~N[2020-03-14 08:54:51Z]) == false + end + + @tag task_id: 5 + test "2019-04-28 was a Sunday" do + assert LibraryFees.monday?(~N[2019-04-28 11:37:12Z]) == false + end + end + + describe "calculate_late_fee/2" do + @tag task_id: 6 + test "returns 0 if the book was returned less than 28 days after a morning checkout" do + result = LibraryFees.calculate_late_fee("2018-11-01T09:00:00Z", "2018-11-13T14:12:00Z", 123) + assert result == 0 + end + + @tag task_id: 6 + test "returns 0 if the book was returned exactly 28 days after a morning checkout" do + result = LibraryFees.calculate_late_fee("2018-11-01T09:00:00Z", "2018-11-29T14:12:00Z", 123) + assert result == 0 + end + + @tag task_id: 6 + test "returns the rate for one day if the book was returned exactly 29 days after a morning checkout" do + result = LibraryFees.calculate_late_fee("2018-11-01T09:00:00Z", "2018-11-30T14:12:00Z", 320) + assert result == 320 + end + + @tag task_id: 6 + test "returns 0 if the book was returned less than 29 days after an afternoon checkout" do + result = LibraryFees.calculate_late_fee("2019-05-01T16:12:00Z", "2019-05-17T14:32:45Z", 400) + assert result == 0 + end + + @tag task_id: 6 + test "returns 0 if the book was returned exactly 29 days after an afternoon checkout" do + result = LibraryFees.calculate_late_fee("2019-05-01T16:12:00Z", "2019-05-30T14:32:45Z", 313) + assert result == 0 + end + + @tag task_id: 6 + test "returns the rate for one day if the book was returned exactly 30 days after an afternoon checkout" do + result = LibraryFees.calculate_late_fee("2019-05-01T16:12:00Z", "2019-05-31T14:32:45Z", 234) + assert result == 234 + end + + @tag task_id: 6 + test "multiplies the number of days late by the rate for one day" do + result = LibraryFees.calculate_late_fee("2021-01-01T08:00:00Z", "2021-02-13T08:00:00Z", 111) + assert result == 111 * 15 + end + + @tag task_id: 6 + test "late fees are 50% off (rounded down) when the book is returned on a Monday" do + result = LibraryFees.calculate_late_fee("2021-01-01T08:00:00Z", "2021-02-15T08:00:00Z", 111) + assert result == trunc(111 * 17 * 0.5) + end + end +end diff --git a/elixir/library-fees/test/test_helper.exs b/elixir/library-fees/test/test_helper.exs new file mode 100644 index 0000000..e8677a3 --- /dev/null +++ b/elixir/library-fees/test/test_helper.exs @@ -0,0 +1,2 @@ +ExUnit.start() +ExUnit.configure(exclude: :pending, trace: true, seed: 0)