Avatar of axtens

axtens's solution

to Allergies in the C# Track

Published at Oct 02 2019 · 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.

Hints

This exercise requires you to use bitwise operations. For more information, see this page.

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 2.0.0 of the canonical data.

using Xunit;

public class AllergiesTest
{
    [Fact]
    public void Testing_for_eggs_allergy_not_allergic_to_anything()
    {
        var sut = new Allergies(0);
        Assert.False(sut.IsAllergicTo(Allergen.Eggs));
    }

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

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

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

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

    [Fact(Skip = "Remove to run test")]
    public void Testing_for_peanuts_allergy_not_allergic_to_anything()
    {
        var sut = new Allergies(0);
        Assert.False(sut.IsAllergicTo(Allergen.Peanuts));
    }

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

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

    [Fact(Skip = "Remove to run test")]
    public void Testing_for_peanuts_allergy_allergic_to_something_but_not_peanuts()
    {
        var sut = new Allergies(5);
        Assert.False(sut.IsAllergicTo(Allergen.Peanuts));
    }

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

    [Fact(Skip = "Remove to run test")]
    public void Testing_for_shellfish_allergy_not_allergic_to_anything()
    {
        var sut = new Allergies(0);
        Assert.False(sut.IsAllergicTo(Allergen.Shellfish));
    }

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

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

    [Fact(Skip = "Remove to run test")]
    public void Testing_for_shellfish_allergy_allergic_to_something_but_not_shellfish()
    {
        var sut = new Allergies(10);
        Assert.False(sut.IsAllergicTo(Allergen.Shellfish));
    }

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

    [Fact(Skip = "Remove to run test")]
    public void Testing_for_strawberries_allergy_not_allergic_to_anything()
    {
        var sut = new Allergies(0);
        Assert.False(sut.IsAllergicTo(Allergen.Strawberries));
    }

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

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

    [Fact(Skip = "Remove to run test")]
    public void Testing_for_strawberries_allergy_allergic_to_something_but_not_strawberries()
    {
        var sut = new Allergies(20);
        Assert.False(sut.IsAllergicTo(Allergen.Strawberries));
    }

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

    [Fact(Skip = "Remove to run test")]
    public void Testing_for_tomatoes_allergy_not_allergic_to_anything()
    {
        var sut = new Allergies(0);
        Assert.False(sut.IsAllergicTo(Allergen.Tomatoes));
    }

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

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

    [Fact(Skip = "Remove to run test")]
    public void Testing_for_tomatoes_allergy_allergic_to_something_but_not_tomatoes()
    {
        var sut = new Allergies(40);
        Assert.False(sut.IsAllergicTo(Allergen.Tomatoes));
    }

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

    [Fact(Skip = "Remove to run test")]
    public void Testing_for_chocolate_allergy_not_allergic_to_anything()
    {
        var sut = new Allergies(0);
        Assert.False(sut.IsAllergicTo(Allergen.Chocolate));
    }

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

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

    [Fact(Skip = "Remove to run test")]
    public void Testing_for_chocolate_allergy_allergic_to_something_but_not_chocolate()
    {
        var sut = new Allergies(80);
        Assert.False(sut.IsAllergicTo(Allergen.Chocolate));
    }

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

    [Fact(Skip = "Remove to run test")]
    public void Testing_for_pollen_allergy_not_allergic_to_anything()
    {
        var sut = new Allergies(0);
        Assert.False(sut.IsAllergicTo(Allergen.Pollen));
    }

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

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

    [Fact(Skip = "Remove to run test")]
    public void Testing_for_pollen_allergy_allergic_to_something_but_not_pollen()
    {
        var sut = new Allergies(160);
        Assert.False(sut.IsAllergicTo(Allergen.Pollen));
    }

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

    [Fact(Skip = "Remove to run test")]
    public void Testing_for_cats_allergy_not_allergic_to_anything()
    {
        var sut = new Allergies(0);
        Assert.False(sut.IsAllergicTo(Allergen.Cats));
    }

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

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

    [Fact(Skip = "Remove to run test")]
    public void Testing_for_cats_allergy_allergic_to_something_but_not_cats()
    {
        var sut = new Allergies(64);
        Assert.False(sut.IsAllergicTo(Allergen.Cats));
    }

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

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

    [Fact(Skip = "Remove to run test")]
    public void 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 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 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 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 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 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 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 No_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;
using System.Collections.Generic;

[Flags]
public enum Allergen
{
    Eggs = 0b1,
    Peanuts = 0b10,
    Shellfish = 0b100,
    Strawberries = 0b1000,
    Tomatoes = 0b10000,
    Chocolate = 0b100000,
    Pollen = 0b1000000,
    Cats = 0b10000000
}

public class Allergies
{
    readonly Allergen knownAllergens = 0;
    public Allergies(int mask)
    {
        knownAllergens = (Allergen)mask;
    }

    public bool IsAllergicTo(Allergen allergen)
    {
        return (knownAllergens & allergen) == allergen;
    }

    public Allergen[] List()
    {
        var allergens = Enum.GetValues(typeof(Allergen)).Cast<Allergen>();
        return allergens
            .Where(IsAllergicTo)
            .ToArray<Allergen>();
    }
}

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?