🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of davearonson

davearonson's solution

to Allergies in the Elixir Track

Published at Jul 13 2018 · 0 comments
Test suite


This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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.

Running tests

Execute the tests with:

$ elixir allergies_test.exs

Pending tests

In the test suites, all but the first test have been 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:

# @tag :pending
test "shouting" do
  assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"

Or, you can enable all the tests by commenting out the ExUnit.configure line in the test suite.

# ExUnit.configure exclude: :pending, trace: true

For more detailed information about the Elixir track, please see the help page.


Jumpstart Lab Warm-up http://jumpstartlab.com

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


if !System.get_env("EXERCISM_TEST_EXAMPLES") do
  Code.load_file("allergies.exs", __DIR__)

ExUnit.configure(exclude: :pending, trace: true)

defmodule AllergiesTest do
  use ExUnit.Case

  # @tag :pending
  test "no_allergies_at_all" do
    Allergies.list(0) |> assert_is_a_set_containing([])

  @tag :pending
  test "allergic_to_just_eggs" do
    Allergies.list(1) |> assert_is_a_set_containing(~w[eggs])

  @tag :pending
  test "allergic_to_just_peanuts" do
    Allergies.list(2) |> assert_is_a_set_containing(~w[peanuts])

  @tag :pending
  test "allergic_to_just_strawberries" do
    Allergies.list(8) |> assert_is_a_set_containing(~w[strawberries])

  @tag :pending
  test "allergic_to_eggs_and_peanuts" do
    Allergies.list(3) |> assert_is_a_set_containing(~w[eggs peanuts])

  @tag :pending
  test "allergic_to_more_than_eggs_but_not_peanuts" do
    Allergies.list(5) |> assert_is_a_set_containing(~w[eggs shellfish])

  @tag :pending
  test "allergic_to_lots_of_stuff" do
    |> assert_is_a_set_containing(~w[strawberries tomatoes chocolate pollen cats])

  @tag :pending
  test "allergic_to_everything" do
    |> assert_is_a_set_containing(
      ~w[eggs peanuts shellfish strawberries tomatoes chocolate pollen cats]

  @tag :pending
  test "no_allergies_means_not_allergic" do
    refute Allergies.allergic_to?(0, "peanuts")
    refute Allergies.allergic_to?(0, "cats")
    refute Allergies.allergic_to?(0, "strawberries")

  @tag :pending
  test "is_allergic_to_eggs" do
    assert Allergies.allergic_to?(1, "eggs")

  @tag :pending
  test "allergic_to_eggs_in_addition_to_other_stuff" do
    assert Allergies.allergic_to?(5, "eggs")

  @tag :pending
  test "ignore_non_allergen_score_parts" do
    |> assert_is_a_set_containing(~w[eggs shellfish strawberries tomatoes chocolate pollen cats])

  defp assert_is_a_set_containing(list, to_contain) do
    set = Enum.into(list, MapSet.new())

    same_contents =
      |> Enum.into(MapSet.new())
      |> MapSet.equal?(set)

    assert same_contents,
           "Expected a set with: #{inspect(to_contain)} got #{inspect(set |> MapSet.to_list())}"
defmodule Allergies do

  use Bitwise

  @allergies \
    ~w(eggs peanuts shellfish strawberries tomatoes chocolate pollen cats)

  @doc """
  List the allergies for which the corresponding flag bit is true.
  @spec list(non_neg_integer) :: [String.t]
  def list(flags) do
    do_list(flags, @allergies, [])

  defp do_list(flags, [allergen|more], acc) when rem(flags, 2) == 1 do
    do_list(flags >>> 1, more, [allergen|acc])
  defp do_list(flags, [_|more], acc), do: do_list(flags >>> 1, more, acc)
  defp do_list(_    , []      , acc), do: acc

  @doc """
  Returns whether the corresponding flag bit in 'flags' is set for the item.
  @spec allergic_to?(non_neg_integer, String.t) :: boolean
  def allergic_to?(flags, item) do
    # pick one of the two below implementations;
    # the first is more elixirish but the second
    # seems likely to be more efficient.
    # do_allergic_to?(flags, item, @allergies)
                    Enum.find_index(@allergies, fn(x) -> x == item end))

  defp do_allergic_to?(flags, item, [item|_]), do: rem(flags, 2) == 1
  defp do_allergic_to?(flags, item, [_|more])  do
    do_allergic_to?(flags, item, more)
  defp do_allergic_to?(_    , _   , []      ), do: false

  # this case was not in the tests but we should cover it anyway
  defp do_allergic_to?(_    , nil), do: false
  defp do_allergic_to?(flags, idx), do: (flags &&& (1 <<< idx)) != 0


Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?