# artemkorsakov's solution

## to Allergies in the C# Track

Published at Mar 31 2019 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution 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 the tests

To run the tests, run the command `dotnet test` from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the `Skip` property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using `exercism submit Allergies.cs`

## Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

## Source

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

### AllergiesTest.cs

``````// This file was auto-generated based on version 1.2.0 of the canonical data.

using Xunit;

public class AllergiesTest
{
[Fact]
public void No_allergies_means_not_allergic()
{
var sut = new Allergies(0);
Assert.False(sut.IsAllergicTo(Allergen.Peanuts));
Assert.False(sut.IsAllergicTo(Allergen.Cats));
Assert.False(sut.IsAllergicTo(Allergen.Strawberries));
}

[Fact(Skip = "Remove to run test")]
public void Is_allergic_to_eggs()
{
var sut = new Allergies(1);
Assert.True(sut.IsAllergicTo(Allergen.Eggs));
}

[Fact(Skip = "Remove to run test")]
{
var sut = new Allergies(5);
Assert.True(sut.IsAllergicTo(Allergen.Eggs));
Assert.True(sut.IsAllergicTo(Allergen.Shellfish));
Assert.False(sut.IsAllergicTo(Allergen.Strawberries));
}

[Fact(Skip = "Remove to run test")]
public void Allergic_to_strawberries_but_not_peanuts()
{
var sut = new Allergies(9);
Assert.True(sut.IsAllergicTo(Allergen.Eggs));
Assert.False(sut.IsAllergicTo(Allergen.Peanuts));
Assert.False(sut.IsAllergicTo(Allergen.Shellfish));
Assert.True(sut.IsAllergicTo(Allergen.Strawberries));
}

[Fact(Skip = "Remove to run test")]
public void No_allergies_at_all()
{
var sut = new Allergies(0);
Assert.Empty(sut.List());
}

[Fact(Skip = "Remove to run test")]
public void Allergic_to_just_eggs()
{
var sut = new Allergies(1);
var expected = new[] { Allergen.Eggs };
Assert.Equal(expected, sut.List());
}

[Fact(Skip = "Remove to run test")]
public void Allergic_to_just_peanuts()
{
var sut = new Allergies(2);
var expected = new[] { Allergen.Peanuts };
Assert.Equal(expected, sut.List());
}

[Fact(Skip = "Remove to run test")]
public void Allergic_to_just_strawberries()
{
var sut = new Allergies(8);
var expected = new[] { Allergen.Strawberries };
Assert.Equal(expected, sut.List());
}

[Fact(Skip = "Remove to run test")]
public void Allergic_to_eggs_and_peanuts()
{
var sut = new Allergies(3);
var expected = new[] { Allergen.Eggs, Allergen.Peanuts };
Assert.Equal(expected, sut.List());
}

[Fact(Skip = "Remove to run test")]
public void Allergic_to_more_than_eggs_but_not_peanuts()
{
var sut = new Allergies(5);
var expected = new[] { Allergen.Eggs, Allergen.Shellfish };
Assert.Equal(expected, sut.List());
}

[Fact(Skip = "Remove to run test")]
public void Allergic_to_lots_of_stuff()
{
var sut = new Allergies(248);
var expected = new[] { Allergen.Strawberries, Allergen.Tomatoes, Allergen.Chocolate, Allergen.Pollen, Allergen.Cats };
Assert.Equal(expected, sut.List());
}

[Fact(Skip = "Remove to run test")]
public void Allergic_to_everything()
{
var sut = new Allergies(255);
var expected = new[] { Allergen.Eggs, Allergen.Peanuts, Allergen.Shellfish, Allergen.Strawberries, Allergen.Tomatoes, Allergen.Chocolate, Allergen.Pollen, Allergen.Cats };
Assert.Equal(expected, sut.List());
}

[Fact(Skip = "Remove to run test")]
public void Ignore_non_allergen_score_parts()
{
var sut = new Allergies(509);
var expected = new[] { Allergen.Eggs, Allergen.Shellfish, Allergen.Strawberries, Allergen.Tomatoes, Allergen.Chocolate, Allergen.Pollen, Allergen.Cats };
Assert.Equal(expected, sut.List());
}
}``````
``````using System;
using System.Linq;

public enum Allergen
{
Eggs,
Peanuts,
Shellfish,
Strawberries,
Tomatoes,
Chocolate,
Pollen,
Cats
}

public class Allergies
{

{
var binary = Convert.ToString(mask % 256, 2);
_allergens = Enumerable.Range(0, Math.Min(binary.Length, 8))
.Where(i => binary[binary.Length - 1 - i] == '1')
.Select(i => (Allergen)i)
.ToArray();
}

public bool IsAllergicTo(Allergen allergen)
{
return _allergens.Contains(allergen);
}

public Allergen[] List()
{
return _allergens;
}
}``````