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:
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:
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.
This exercise requires you to use bitwise operations. For more information, see this page.
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
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.
Jumpstart Lab Warm-up http://jumpstartlab.com
// 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")]
public void Allergic_to_eggs_in_addition_to_other_stuff()
{
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.Collections.Generic;
using System.Linq;
[Flags]
public enum Allergen
{
Eggs = 1,
Peanuts = 2,
Shellfish = 4,
Strawberries = 8,
Tomatoes = 16,
Chocolate = 32,
Pollen = 64,
Cats = 128
}
public class Allergies
{
private readonly Allergen myAllergies;
public Allergies(int mask)
{
myAllergies = (Allergen)mask;
}
public bool IsAllergicTo(Allergen allergen)
{
return myAllergies.HasFlag(allergen);
}
public Allergen[] List()
{
return Enum.GetValues(typeof(Allergen))
.Cast<Allergen>()
.Where(x => IsAllergicTo(x))
.ToArray();
}
}
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.
Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments