From 6bcb4db837bbaba29891e60b516f2215d182a90d Mon Sep 17 00:00:00 2001 From: Danylo Negrienko Date: Wed, 21 Aug 2024 23:14:37 -0400 Subject: [PATCH] allergies --- elixir/allergies/.exercism/config.json | 37 ++++ elixir/allergies/.exercism/metadata.json | 1 + elixir/allergies/.formatter.exs | 4 + elixir/allergies/.gitignore | 24 +++ elixir/allergies/HELP.md | 75 +++++++ elixir/allergies/README.md | 61 ++++++ elixir/allergies/lib/allergies.ex | 27 +++ elixir/allergies/mix.exs | 28 +++ elixir/allergies/test/allergies_test.exs | 241 +++++++++++++++++++++++ elixir/allergies/test/test_helper.exs | 2 + 10 files changed, 500 insertions(+) create mode 100644 elixir/allergies/.exercism/config.json create mode 100644 elixir/allergies/.exercism/metadata.json create mode 100644 elixir/allergies/.formatter.exs create mode 100644 elixir/allergies/.gitignore create mode 100644 elixir/allergies/HELP.md create mode 100644 elixir/allergies/README.md create mode 100644 elixir/allergies/lib/allergies.ex create mode 100644 elixir/allergies/mix.exs create mode 100644 elixir/allergies/test/allergies_test.exs create mode 100644 elixir/allergies/test/test_helper.exs diff --git a/elixir/allergies/.exercism/config.json b/elixir/allergies/.exercism/config.json new file mode 100644 index 0000000..2540cdc --- /dev/null +++ b/elixir/allergies/.exercism/config.json @@ -0,0 +1,37 @@ +{ + "authors": [ + "rubysolo" + ], + "contributors": [ + "andrewsardone", + "angelikatyborska", + "Cohen-Carlisle", + "dalexj", + "devonestes", + "ggpasqualino", + "jinyeow", + "lpil", + "martinsvalin", + "meadsteve", + "montague", + "neenjaw", + "parkerl", + "sotojuan", + "Teapane", + "waiting-for-dev" + ], + "files": { + "solution": [ + "lib/allergies.ex" + ], + "test": [ + "test/allergies_test.exs" + ], + "example": [ + ".meta/example.ex" + ] + }, + "blurb": "Given a person's allergy score, determine whether or not they're allergic to a given item, and their full list of allergies.", + "source": "Exercise by the JumpstartLab team for students at The Turing School of Software and Design.", + "source_url": "https://turing.edu" +} diff --git a/elixir/allergies/.exercism/metadata.json b/elixir/allergies/.exercism/metadata.json new file mode 100644 index 0000000..48690f3 --- /dev/null +++ b/elixir/allergies/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"elixir","exercise":"allergies","id":"0f056a8c570349d4b0832bbf025c5c86","url":"https://exercism.org/tracks/elixir/exercises/allergies","handle":"negrienko","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/elixir/allergies/.formatter.exs b/elixir/allergies/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/elixir/allergies/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/elixir/allergies/.gitignore b/elixir/allergies/.gitignore new file mode 100644 index 0000000..456b4dd --- /dev/null +++ b/elixir/allergies/.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"). +allergies-*.tar + diff --git a/elixir/allergies/HELP.md b/elixir/allergies/HELP.md new file mode 100644 index 0000000..4125f8b --- /dev/null +++ b/elixir/allergies/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/allergies.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/allergies/README.md b/elixir/allergies/README.md new file mode 100644 index 0000000..a7f3728 --- /dev/null +++ b/elixir/allergies/README.md @@ -0,0 +1,61 @@ +# Allergies + +Welcome to Allergies on Exercism's Elixir Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Instructions + +Given a person's allergy score, determine whether or not they're allergic to a given item, and their full list of allergies. + +An allergy test produces a single numeric score which contains the information about all the allergies the person has (that they were tested for). + +The list of items (and their value) that were tested are: + +- eggs (1) +- peanuts (2) +- shellfish (4) +- strawberries (8) +- tomatoes (16) +- chocolate (32) +- pollen (64) +- cats (128) + +So if Tom is allergic to peanuts and chocolate, he gets a score of 34. + +Now, given just that score of 34, your program should be able to say: + +- Whether Tom is allergic to any one of those allergens listed above. +- All the allergens Tom is allergic to. + +Note: a given score may include allergens **not** listed above (i.e. allergens that score 256, 512, 1024, etc.). +Your program should ignore those components of the score. +For example, if the allergy score is 257, your program should only report the eggs (1) allergy. + +## Source + +### Created by + +- @rubysolo + +### Contributed to by + +- @andrewsardone +- @angelikatyborska +- @Cohen-Carlisle +- @dalexj +- @devonestes +- @ggpasqualino +- @jinyeow +- @lpil +- @martinsvalin +- @meadsteve +- @montague +- @neenjaw +- @parkerl +- @sotojuan +- @Teapane +- @waiting-for-dev + +### Based on + +Exercise by the JumpstartLab team for students at The Turing School of Software and Design. - https://turing.edu \ No newline at end of file diff --git a/elixir/allergies/lib/allergies.ex b/elixir/allergies/lib/allergies.ex new file mode 100644 index 0000000..59d92c9 --- /dev/null +++ b/elixir/allergies/lib/allergies.ex @@ -0,0 +1,27 @@ +defmodule Allergies do + @allergens %{ + "eggs" => 1, + "peanuts" => 2, + "shellfish" => 4, + "strawberries" => 8, + "tomatoes" => 16, + "chocolate" => 32, + "pollen" => 64, + "cats" => 128 + } + + @doc """ + List the allergies for which the corresponding flag bit is true. + """ + @spec list(non_neg_integer) :: [String.t()] + def list(flags) do + for {allergen, mask} <- @allergens, allergic_to?(mask, flags), do: allergen + end + + @doc """ + Returns whether the corresponding flag bit in 'flags' is set for the item. + """ + @spec allergic_to?(non_neg_integer, String.t() | non_neg_integer) :: boolean + def allergic_to?(flags, mask) when is_integer(mask), do: Bitwise.band(mask, flags) > 0 + def allergic_to?(flags, allergen) when is_binary(allergen), do: allergic_to?(@allergens[allergen], flags) +end diff --git a/elixir/allergies/mix.exs b/elixir/allergies/mix.exs new file mode 100644 index 0000000..e5b7c6c --- /dev/null +++ b/elixir/allergies/mix.exs @@ -0,0 +1,28 @@ +defmodule Allergies.MixProject do + use Mix.Project + + def project do + [ + app: :allergies, + 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/allergies/test/allergies_test.exs b/elixir/allergies/test/allergies_test.exs new file mode 100644 index 0000000..e9ea8b6 --- /dev/null +++ b/elixir/allergies/test/allergies_test.exs @@ -0,0 +1,241 @@ +defmodule AllergiesTest do + use ExUnit.Case + + defp assert_is_a_set_containing(list, to_contain) do + set = Enum.into(list, MapSet.new()) + + same_contents = + to_contain + |> Enum.into(MapSet.new()) + |> MapSet.equal?(set) + + assert same_contents, + "Expected a set with: #{inspect(to_contain)} got #{inspect(set |> MapSet.to_list())}" + end + + describe "allergy list against expected allergens -" do + test "no allergies at all" do + Allergies.list(0) |> assert_is_a_set_containing([]) + end + + test "allergic to just eggs" do + Allergies.list(1) |> assert_is_a_set_containing(~w[eggs]) + end + + test "allergic to just peanuts" do + Allergies.list(2) |> assert_is_a_set_containing(~w[peanuts]) + end + + test "allergic to just strawberries" do + Allergies.list(8) |> assert_is_a_set_containing(~w[strawberries]) + end + + test "allergic to eggs and peanuts" do + Allergies.list(3) |> assert_is_a_set_containing(~w[eggs peanuts]) + end + + test "allergic to more than eggs but not peanuts" do + Allergies.list(5) |> assert_is_a_set_containing(~w[eggs shellfish]) + end + + test "allergic to lots of stuff" do + Allergies.list(248) + |> assert_is_a_set_containing(~w[strawberries tomatoes chocolate pollen cats]) + end + + test "allergic to everything" do + Allergies.list(255) + |> assert_is_a_set_containing( + ~w[eggs peanuts shellfish strawberries tomatoes chocolate pollen cats] + ) + end + + test "ignore non allergen score parts" do + Allergies.list(509) + |> assert_is_a_set_containing( + ~w[eggs shellfish strawberries tomatoes chocolate pollen cats] + ) + end + + test "ignore non allergen score parts without highest valid score" do + Allergies.list(257) + |> assert_is_a_set_containing(~w[eggs]) + end + end + + describe "score for egg allergies -" do + test "not allergic to eggs" do + refute Allergies.allergic_to?(0, "eggs") + end + + test "is allergic to only eggs" do + assert Allergies.allergic_to?(1, "eggs") + end + + test "is allergic to eggs and something else" do + assert Allergies.allergic_to?(3, "eggs") + end + + test "is allergic to something, but not eggs" do + refute Allergies.allergic_to?(2, "eggs") + end + + test "is allergic to everything (including eggs)" do + assert Allergies.allergic_to?(255, "eggs") + end + end + + describe "score for peanuts allergies -" do + test "not allergic to peanuts" do + refute Allergies.allergic_to?(0, "peanuts") + end + + test "is allergic to only peanuts" do + assert Allergies.allergic_to?(2, "peanuts") + end + + test "is allergic to peanuts and something else" do + assert Allergies.allergic_to?(7, "peanuts") + end + + test "is allergic to something, but not peanuts" do + refute Allergies.allergic_to?(5, "peanuts") + end + + test "is allergic to everything (including peanuts)" do + assert Allergies.allergic_to?(255, "peanuts") + end + end + + describe "score for shellfish allergies -" do + test "not allergic to shellfish" do + refute Allergies.allergic_to?(0, "shellfish") + end + + test "is allergic to only shellfish" do + assert Allergies.allergic_to?(4, "shellfish") + end + + test "is allergic to shellfish and something else" do + assert Allergies.allergic_to?(14, "shellfish") + end + + test "is allergic to something, but not shellfish" do + refute Allergies.allergic_to?(10, "shellfish") + end + + test "is allergic to everything (including shellfish)" do + assert Allergies.allergic_to?(255, "shellfish") + end + end + + describe "score for strawberries allergies -" do + test "not allergic to strawberries" do + refute Allergies.allergic_to?(0, "strawberries") + end + + test "is allergic to only strawberries" do + assert Allergies.allergic_to?(8, "strawberries") + end + + test "is allergic to strawberries and something else" do + assert Allergies.allergic_to?(28, "strawberries") + end + + test "is allergic to something, but not strawberries" do + refute Allergies.allergic_to?(20, "strawberries") + end + + test "is allergic to everything (including strawberries)" do + assert Allergies.allergic_to?(255, "strawberries") + end + end + + describe "score for tomatoes allergies -" do + test "not allergic to tomatoes" do + refute Allergies.allergic_to?(0, "tomatoes") + end + + test "is allergic to only tomatoes" do + assert Allergies.allergic_to?(16, "tomatoes") + end + + test "is allergic to tomatoes and something else" do + assert Allergies.allergic_to?(56, "tomatoes") + end + + test "is allergic to something, but not tomatoes" do + refute Allergies.allergic_to?(40, "tomatoes") + end + + test "is allergic to everything (including tomatoes)" do + assert Allergies.allergic_to?(255, "tomatoes") + end + end + + describe "score for chocolate allergies -" do + test "not allergic to chocolate" do + refute Allergies.allergic_to?(0, "chocolate") + end + + test "is allergic to only chocolate" do + assert Allergies.allergic_to?(32, "chocolate") + end + + test "is allergic to chocolate and something else" do + assert Allergies.allergic_to?(112, "chocolate") + end + + test "is allergic to something, but not chocolate" do + refute Allergies.allergic_to?(80, "chocolate") + end + + test "is allergic to everything (including chocolate)" do + assert Allergies.allergic_to?(255, "chocolate") + end + end + + describe "score for pollen allergies -" do + test "not allergic to pollen" do + refute Allergies.allergic_to?(0, "pollen") + end + + test "is allergic to only pollen" do + assert Allergies.allergic_to?(64, "pollen") + end + + test "is allergic to pollen and something else" do + assert Allergies.allergic_to?(224, "pollen") + end + + test "is allergic to something, but not pollen" do + refute Allergies.allergic_to?(160, "pollen") + end + + test "is allergic to everything (including pollen)" do + assert Allergies.allergic_to?(255, "pollen") + end + end + + describe "score for cats allergies -" do + test "not allergic to cats" do + refute Allergies.allergic_to?(0, "cats") + end + + test "is allergic to only cats" do + assert Allergies.allergic_to?(128, "cats") + end + + test "is allergic to cats and something else" do + assert Allergies.allergic_to?(192, "cats") + end + + test "is allergic to something, but not cats" do + refute Allergies.allergic_to?(64, "cats") + end + + test "is allergic to everything (including cats)" do + assert Allergies.allergic_to?(255, "cats") + end + end +end diff --git a/elixir/allergies/test/test_helper.exs b/elixir/allergies/test/test_helper.exs new file mode 100644 index 0000000..35fc5bf --- /dev/null +++ b/elixir/allergies/test/test_helper.exs @@ -0,0 +1,2 @@ +ExUnit.start() +ExUnit.configure(exclude: :pending, trace: true)