# 4d47's solution

## to Allergies in the Perl 6 Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

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.

## Resources

Remember to check out the Perl 6 documentation and resources pages for information, tips, and examples if you get stuck.

## Running the tests

There is a test suite and module included with the exercise. The test suite (a file with the extension `.t`) will attempt to run routines from the module (a file with the extension `.pm6`). Add/modify routines in the module so that the tests will pass! You can view the test data by executing the command `perl6 --doc *.t` (* being the name of the test suite), and run the test suite for the exercise by executing the command `prove6 .` in the exercise directory.

## 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.t

``````#!/usr/bin/env perl6
use v6;
use Test;
use JSON::Fast;
use lib \$?FILE.IO.dirname;
use Allergies;
plan 2;

my \$c-data = from-json \$=pod.pop.contents;
for \$c-data<cases>.values -> %case-set {

subtest 'allergic-to' => {
plan 7;
for %case-set<cases>.values -> %case {
for %case<expected>.values {
given allergic-to %case<input><score>, .<substance> -> \$result {
subtest %case<description> ~ ': ' ~ .<substance> => {
plan 2;
isa-ok \$result, Bool;
is-deeply \$result, .<result>, 'Result matches expected';
}
}
}
}
} when %case-set<description> ~~ 'allergicTo';

subtest 'list-allergies' => {
plan 9;
for %case-set<cases>.values {
cmp-ok list-allergies(.<input><score>), '~~', .<expected>.Set, .<description>;
}
} when %case-set<description> ~~ 'list';

}

=begin code
{
"exercise": "allergies",
"version": "1.1.0",
"cases": [
{
"description": "allergicTo",
"Given a number and a substance, indicate whether Tom is allergic ",
"to that substance.",
"Test cases for this method involve more than one assertion.",
"Each case in 'expected' specifies what the method should return for",
"the given substance."
],
"cases": [
{
"description": "no allergies means not allergic",
"property": "allergicTo",
"input": {
"score": 0
},
"expected": [
{
"substance": "peanuts",
"result": false
},
{
"substance": "cats",
"result": false
},
{
"substance": "strawberries",
"result": false
}
]
},
{
"description": "is allergic to eggs",
"property": "allergicTo",
"input": {
"score": 1
},
"expected": [
{
"substance": "eggs",
"result": true
}
]
},
{
"description": "allergic to eggs in addition to other stuff",
"property": "allergicTo",
"input": {
"score": 5
},
"expected": [
{
"substance": "eggs",
"result": true
},
{
"substance": "shellfish",
"result": true
},
{
"substance": "strawberries",
"result": false
}
]
}
]
},
{
"description": "list",
"Given a number, list all things Tom is allergic to"
],
"cases": [
{
"description": "no allergies at all",
"property": "list",
"input": {
"score": 0
},
"expected": []
},
{
"description": "allergic to just eggs",
"property": "list",
"input": {
"score": 1
},
"expected": ["eggs"]
},
{
"description": "allergic to just peanuts",
"property": "list",
"input": {
"score": 2
},
"expected": ["peanuts"]
},
{
"description": "allergic to just strawberries",
"property": "list",
"input": {
"score": 8
},
"expected": ["strawberries"]
},
{
"description": "allergic to eggs and peanuts",
"property": "list",
"input": {
"score": 3
},
"expected": ["eggs", "peanuts"]
},
{
"description": "allergic to more than eggs but not peanuts",
"property": "list",
"input": {
"score": 5
},
"expected": ["eggs", "shellfish"]
},
{
"description": "allergic to lots of stuff",
"property": "list",
"input": {
"score": 248
},
"expected": [ "strawberries",
"tomatoes",
"chocolate",
"pollen",
"cats"
]
},
{
"description": "allergic to everything",
"property": "list",
"input": {
"score": 255
},
"expected": [ "eggs",
"peanuts",
"shellfish",
"strawberries",
"tomatoes",
"chocolate",
"pollen",
"cats"
]
},
{
"description": "ignore non allergen score parts",
"property": "list",
"input": {
"score": 509
},
"expected": [ "eggs",
"shellfish",
"strawberries",
"tomatoes",
"chocolate",
"pollen",
"cats"
]
}
]
}
]
}
=end code``````
``````my @allergies =
:1eggs,
:2peanuts,
:4shellfish,
:8strawberries,
:16tomatoes,
:32chocolate,
:64pollen,
:128cats;

sub allergic-to(UInt \$score, Str \$substance --> Bool) is export {
so \$score +& %@allergies{\$substance}
}

sub list-allergies(UInt \$score --> Seq) is export {
@allergies».key.grep: { allergic-to(\$score, \$^substance) }
}``````