ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# EmilienMottet's solution

## to Allergies in the Erlang Track

Published at Nov 11 2020 · 0 comments
Instructions
Test suite
Solution

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

In order to run the tests, issue the following command from the exercise directory:

For running the tests provided, `rebar3` is used as it is the official build and dependency management tool for erlang now. Please refer to the tracks installation instructions on how to do that.

In order to run the tests, you can issue the following command from the exercise directory.

``````\$ rebar3 eunit
``````

## Questions?

For detailed information about the Erlang track, please refer to the help page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

## Source

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.

### allergies_tests.erl

``````%% Generated with 'testgen v0.2.0'
%% Revision 2 of the exercises generator was used
%% https://github.com/exercism/problem-specifications/raw/42dd0cea20498fd544b152c4e2c0a419bb7e266a/exercises/allergies/canonical-data.json
%% This file is automatically generated from the exercises canonical data.

-module(allergies_tests).

-include_lib("erl_exercism/include/exercism.hrl").
-include_lib("eunit/include/eunit.hrl").

'1_not_allergic_to_anything_test_'() ->
Score = 0,
{"not allergic to anything",
?_assertNot(allergies:is_allergic_to(eggs, Score))}.

'2_allergic_only_to_eggs_test_'() ->
Score = 1,
{"allergic only to eggs",
?_assert(allergies:is_allergic_to(eggs, Score))}.

'3_allergic_to_eggs_and_something_else_test_'() ->
Score = 3,
{"allergic to eggs and something else",
?_assert(allergies:is_allergic_to(eggs, Score))}.

'4_allergic_to_something_but_not_eggs_test_'() ->
Score = 2,
{"allergic to something, but not eggs",
?_assertNot(allergies:is_allergic_to(eggs, Score))}.

'5_allergic_to_everything_test_'() ->
Score = 255,
{"allergic to everything",
?_assert(allergies:is_allergic_to(eggs, Score))}.

'6_not_allergic_to_anything_test_'() ->
Score = 0,
{"not allergic to anything",
?_assertNot(allergies:is_allergic_to(peanuts, Score))}.

'7_allergic_only_to_peanuts_test_'() ->
Score = 2,
{"allergic only to peanuts",
?_assert(allergies:is_allergic_to(peanuts, Score))}.

'8_allergic_to_peanuts_and_something_else_test_'() ->
Score = 7,
{"allergic to peanuts and something else",
?_assert(allergies:is_allergic_to(peanuts, Score))}.

'9_allergic_to_something_but_not_peanuts_test_'() ->
Score = 5,
{"allergic to something, but not peanuts",
?_assertNot(allergies:is_allergic_to(peanuts, Score))}.

'10_allergic_to_everything_test_'() ->
Score = 255,
{"allergic to everything",
?_assert(allergies:is_allergic_to(peanuts, Score))}.

'11_not_allergic_to_anything_test_'() ->
Score = 0,
{"not allergic to anything",
?_assertNot(allergies:is_allergic_to(shellfish,
Score))}.

'12_allergic_only_to_shellfish_test_'() ->
Score = 4,
{"allergic only to shellfish",
?_assert(allergies:is_allergic_to(shellfish, Score))}.

'13_allergic_to_shellfish_and_something_else_test_'() ->
Score = 14,
{"allergic to shellfish and something "
"else",
?_assert(allergies:is_allergic_to(shellfish, Score))}.

'14_allergic_to_something_but_not_shellfish_test_'() ->
Score = 10,
{"allergic to something, but not shellfish",
?_assertNot(allergies:is_allergic_to(shellfish,
Score))}.

'15_allergic_to_everything_test_'() ->
Score = 255,
{"allergic to everything",
?_assert(allergies:is_allergic_to(shellfish, Score))}.

'16_not_allergic_to_anything_test_'() ->
Score = 0,
{"not allergic to anything",
?_assertNot(allergies:is_allergic_to(strawberries,
Score))}.

'17_allergic_only_to_strawberries_test_'() ->
Score = 8,
{"allergic only to strawberries",
?_assert(allergies:is_allergic_to(strawberries,
Score))}.

'18_allergic_to_strawberries_and_something_else_test_'() ->
Score = 28,
{"allergic to strawberries and something "
"else",
?_assert(allergies:is_allergic_to(strawberries,
Score))}.

'19_allergic_to_something_but_not_strawberries_test_'() ->
Score = 20,
{"allergic to something, but not strawberries",
?_assertNot(allergies:is_allergic_to(strawberries,
Score))}.

'20_allergic_to_everything_test_'() ->
Score = 255,
{"allergic to everything",
?_assert(allergies:is_allergic_to(strawberries,
Score))}.

'21_not_allergic_to_anything_test_'() ->
Score = 0,
{"not allergic to anything",
?_assertNot(allergies:is_allergic_to(tomatoes, Score))}.

'22_allergic_only_to_tomatoes_test_'() ->
Score = 16,
{"allergic only to tomatoes",
?_assert(allergies:is_allergic_to(tomatoes, Score))}.

'23_allergic_to_tomatoes_and_something_else_test_'() ->
Score = 56,
{"allergic to tomatoes and something else",
?_assert(allergies:is_allergic_to(tomatoes, Score))}.

'24_allergic_to_something_but_not_tomatoes_test_'() ->
Score = 40,
{"allergic to something, but not tomatoes",
?_assertNot(allergies:is_allergic_to(tomatoes, Score))}.

'25_allergic_to_everything_test_'() ->
Score = 255,
{"allergic to everything",
?_assert(allergies:is_allergic_to(tomatoes, Score))}.

'26_not_allergic_to_anything_test_'() ->
Score = 0,
{"not allergic to anything",
?_assertNot(allergies:is_allergic_to(chocolate,
Score))}.

'27_allergic_only_to_chocolate_test_'() ->
Score = 32,
{"allergic only to chocolate",
?_assert(allergies:is_allergic_to(chocolate, Score))}.

'28_allergic_to_chocolate_and_something_else_test_'() ->
Score = 112,
{"allergic to chocolate and something "
"else",
?_assert(allergies:is_allergic_to(chocolate, Score))}.

'29_allergic_to_something_but_not_chocolate_test_'() ->
Score = 80,
{"allergic to something, but not chocolate",
?_assertNot(allergies:is_allergic_to(chocolate,
Score))}.

'30_allergic_to_everything_test_'() ->
Score = 255,
{"allergic to everything",
?_assert(allergies:is_allergic_to(chocolate, Score))}.

'31_not_allergic_to_anything_test_'() ->
Score = 0,
{"not allergic to anything",
?_assertNot(allergies:is_allergic_to(pollen, Score))}.

'32_allergic_only_to_pollen_test_'() ->
Score = 64,
{"allergic only to pollen",
?_assert(allergies:is_allergic_to(pollen, Score))}.

'33_allergic_to_pollen_and_something_else_test_'() ->
Score = 224,
{"allergic to pollen and something else",
?_assert(allergies:is_allergic_to(pollen, Score))}.

'34_allergic_to_something_but_not_pollen_test_'() ->
Score = 160,
{"allergic to something, but not pollen",
?_assertNot(allergies:is_allergic_to(pollen, Score))}.

'35_allergic_to_everything_test_'() ->
Score = 255,
{"allergic to everything",
?_assert(allergies:is_allergic_to(pollen, Score))}.

'36_not_allergic_to_anything_test_'() ->
Score = 0,
{"not allergic to anything",
?_assertNot(allergies:is_allergic_to(cats, Score))}.

'37_allergic_only_to_cats_test_'() ->
Score = 128,
{"allergic only to cats",
?_assert(allergies:is_allergic_to(cats, Score))}.

'38_allergic_to_cats_and_something_else_test_'() ->
Score = 192,
{"allergic to cats and something else",
?_assert(allergies:is_allergic_to(cats, Score))}.

'39_allergic_to_something_but_not_cats_test_'() ->
Score = 64,
{"allergic to something, but not cats",
?_assertNot(allergies:is_allergic_to(cats, Score))}.

'40_allergic_to_everything_test_'() ->
Score = 255,
{"allergic to everything",
?_assert(allergies:is_allergic_to(cats, Score))}.

'41_no_allergies_test_'() ->
{"no allergies",
?_assertMatch([], lists:sort(allergies:allergies(0)))}.

'42_just_eggs_test_'() ->
{"just eggs",
?_assertMatch([eggs],
lists:sort(allergies:allergies(1)))}.

'43_just_peanuts_test_'() ->
{"just peanuts",
?_assertMatch([peanuts],
lists:sort(allergies:allergies(2)))}.

'44_just_strawberries_test_'() ->
{"just strawberries",
?_assertMatch([strawberries],
lists:sort(allergies:allergies(8)))}.

'45_eggs_and_peanuts_test_'() ->
{"eggs and peanuts",
?_assertMatch([eggs, peanuts],
lists:sort(allergies:allergies(3)))}.

'46_more_than_eggs_but_not_peanuts_test_'() ->
{"more than eggs but not peanuts",
?_assertMatch([eggs, shellfish],
lists:sort(allergies:allergies(5)))}.

'47_lots_of_stuff_test_'() ->
{"lots of stuff",
?_assertMatch([cats, chocolate, pollen, strawberries,
tomatoes],
lists:sort(allergies:allergies(248)))}.

'48_everything_test_'() ->
{"everything",
?_assertMatch([cats, chocolate, eggs, peanuts, pollen,
shellfish, strawberries, tomatoes],
lists:sort(allergies:allergies(255)))}.

'49_no_allergen_score_parts_test_'() ->
{"no allergen score parts",
?_assertMatch([cats, chocolate, eggs, pollen, shellfish,
strawberries, tomatoes],
lists:sort(allergies:allergies(509)))}.``````
``````-module(allergies).

-export([allergies/1, is_allergic_to/2]).

map_item() ->
#{eggs => 1,
peanuts => 2,
shellfish => 4,
strawberries => 8,
tomatoes => 16,
chocolate => 32,
pollen => 64,
cats => 128}.

allergies(Score) ->
maps:keys(maps:filter(fun (K, _V) ->
is_allergic_to(K, Score)
end,
map_item())).

is_allergic_to(Substance, Score) ->
Value = maps:get(Substance, map_item()),
Score band Value =:= Value.``````