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

n0mn0m's solution

to Allergies in the C# Track

Published at Oct 29 2020 · 3 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 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

AllergiesTests.cs

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

using Xunit;

public class AllergiesTests
{
[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 this Skip property to run this 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 this Skip property to run this 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 this Skip property to run this 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 this Skip property to run this test")]
public void Testing_for_eggs_allergy_allergic_to_everything()
{
var sut = new Allergies(255);
Assert.True(sut.IsAllergicTo(Allergen.Eggs));
}

[Fact(Skip = "Remove this Skip property to run this 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 this Skip property to run this 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 this Skip property to run this 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 this Skip property to run this 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 this Skip property to run this test")]
public void Testing_for_peanuts_allergy_allergic_to_everything()
{
var sut = new Allergies(255);
Assert.True(sut.IsAllergicTo(Allergen.Peanuts));
}

[Fact(Skip = "Remove this Skip property to run this 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 this Skip property to run this 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 this Skip property to run this 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 this Skip property to run this 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 this Skip property to run this test")]
public void Testing_for_shellfish_allergy_allergic_to_everything()
{
var sut = new Allergies(255);
Assert.True(sut.IsAllergicTo(Allergen.Shellfish));
}

[Fact(Skip = "Remove this Skip property to run this 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 this Skip property to run this 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 this Skip property to run this 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 this Skip property to run this 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 this Skip property to run this test")]
public void Testing_for_strawberries_allergy_allergic_to_everything()
{
var sut = new Allergies(255);
Assert.True(sut.IsAllergicTo(Allergen.Strawberries));
}

[Fact(Skip = "Remove this Skip property to run this 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 this Skip property to run this 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 this Skip property to run this 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 this Skip property to run this 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 this Skip property to run this test")]
public void Testing_for_tomatoes_allergy_allergic_to_everything()
{
var sut = new Allergies(255);
Assert.True(sut.IsAllergicTo(Allergen.Tomatoes));
}

[Fact(Skip = "Remove this Skip property to run this 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 this Skip property to run this 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 this Skip property to run this 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 this Skip property to run this 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 this Skip property to run this test")]
public void Testing_for_chocolate_allergy_allergic_to_everything()
{
var sut = new Allergies(255);
Assert.True(sut.IsAllergicTo(Allergen.Chocolate));
}

[Fact(Skip = "Remove this Skip property to run this 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 this Skip property to run this 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 this Skip property to run this 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 this Skip property to run this 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 this Skip property to run this test")]
public void Testing_for_pollen_allergy_allergic_to_everything()
{
var sut = new Allergies(255);
Assert.True(sut.IsAllergicTo(Allergen.Pollen));
}

[Fact(Skip = "Remove this Skip property to run this 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 this Skip property to run this 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 this Skip property to run this 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 this Skip property to run this 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 this Skip property to run this test")]
public void Testing_for_cats_allergy_allergic_to_everything()
{
var sut = new Allergies(255);
Assert.True(sut.IsAllergicTo(Allergen.Cats));
}

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

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

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

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

[Fact(Skip = "Remove this Skip property to run this 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 this Skip property to run this 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 this Skip property to run this 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 this Skip property to run this 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 this Skip property to run this 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;

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

public class Allergies
{

public bool IsAllergicTo(Allergen allergen) => IsBitSet(_mask, (int)allergen);

public Allergen[] List() => Enum.GetValues(typeof(Allergen)).Cast<Allergen>().Where(allergen => IsBitSet(_mask, (int)allergen)).ToArray();

private static bool IsBitSet(int b, int position) => (b & (1 << position)) != 0;
}``````

Hello n0mn0m, take a look at my solution, please let me know what do you think compared against yours.

Solution Author
commented 217 days ago

I like it a lot. The flags attribute definitely makes things easier to read. Nice job.

I like your style, nonetheless. It makes me think of new possibilities.

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?