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

# SergiiVlasiuk's solution

## to Allergies in the Scala Track

Published at Aug 19 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.

The Scala exercises assume an SBT project scheme. The exercise solution source should be placed within the exercise directory/src/main/scala. The exercise unit tests can be found within the exercise directory/src/test/scala.

To run the tests simply run the command `sbt test` in the exercise directory.

For more detailed info about the Scala track see the help page.

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

### AllergiesTest.scala

``````import org.scalatest.{Matchers, FunSuite}

/** @version 1.2.0 */
class AllergiesTest extends FunSuite with Matchers {

test("Allergen.Peanuts - no allergies means not allergic") {
Allergies.allergicTo(Allergen.Peanuts, 0) should be(false)
}

test("Allergen.Cats - no allergies means not allergic") {
pending
Allergies.allergicTo(Allergen.Cats, 0) should be(false)
}

test("Allergen.Strawberries - no allergies means not allergic") {
pending
Allergies.allergicTo(Allergen.Strawberries, 0) should be(false)
}

test("Allergen.Eggs - is allergic to eggs") {
pending
Allergies.allergicTo(Allergen.Eggs, 1) should be(true)
}

test("Allergen.Eggs - allergic to eggs in addition to other stuff") {
pending
Allergies.allergicTo(Allergen.Eggs, 5) should be(true)
}

test("Allergen.Shellfish - allergic to eggs in addition to other stuff") {
pending
Allergies.allergicTo(Allergen.Shellfish, 5) should be(true)
}

test("Allergen.Strawberries - allergic to eggs in addition to other stuff") {
pending
Allergies.allergicTo(Allergen.Strawberries, 5) should be(false)
}

test("Allergen.Eggs - allergic to strawberries but not peanuts") {
pending
Allergies.allergicTo(Allergen.Eggs, 9) should be(true)
}

test("Allergen.Peanuts - allergic to strawberries but not peanuts") {
pending
Allergies.allergicTo(Allergen.Peanuts, 9) should be(false)
}

test("Allergen.Shellfish - allergic to strawberries but not peanuts") {
pending
Allergies.allergicTo(Allergen.Shellfish, 9) should be(false)
}

test("Allergen.Strawberries - allergic to strawberries but not peanuts") {
pending
Allergies.allergicTo(Allergen.Strawberries, 9) should be(true)
}

test("no allergies at all") {
pending
Allergies.list(0) should be(List())
}

test("allergic to just eggs") {
pending
Allergies.list(1) should be(List(Allergen.Eggs))
}

test("allergic to just peanuts") {
pending
Allergies.list(2) should be(List(Allergen.Peanuts))
}

test("allergic to just strawberries") {
pending
Allergies.list(8) should be(List(Allergen.Strawberries))
}

test("allergic to eggs and peanuts") {
pending
Allergies.list(3) should be(List(Allergen.Eggs, Allergen.Peanuts))
}

test("allergic to more than eggs but not peanuts") {
pending
Allergies.list(5) should be(List(Allergen.Eggs, Allergen.Shellfish))
}

test("allergic to lots of stuff") {
pending
Allergies.list(248) should be(
List(Allergen.Strawberries,
Allergen.Tomatoes,
Allergen.Chocolate,
Allergen.Pollen,
Allergen.Cats))
}

test("allergic to everything") {
pending
Allergies.list(255) should be(
List(Allergen.Eggs,
Allergen.Peanuts,
Allergen.Shellfish,
Allergen.Strawberries,
Allergen.Tomatoes,
Allergen.Chocolate,
Allergen.Pollen,
Allergen.Cats))
}

test("ignore non allergen score parts") {
pending
Allergies.list(509) should be(
List(Allergen.Eggs,
Allergen.Shellfish,
Allergen.Strawberries,
Allergen.Tomatoes,
Allergen.Chocolate,
Allergen.Pollen,
Allergen.Cats))
}
}``````
``````object Allergies {
def list(code: Int): List[Allergen] = Allergen.allergens.filter(allergicTo(_, code))

def allergicTo(alergen: Allergen, code: Int): Boolean = (code & alergen.getAllergyNum) != 0
}

abstract sealed class Allergen(allergyNum: Int) {
def getAllergyNum = allergyNum
}

object Allergen {
case object Eggs extends Allergen(1)
case object Peanuts extends Allergen(2)
case object Shellfish extends Allergen(4)
case object Strawberries extends Allergen(8)
case object Tomatoes extends Allergen(16)
case object Chocolate extends Allergen(32)
case object Pollen extends Allergen(64)
case object Cats extends Allergen(128)

val allergens: List[Allergen] = List(Eggs, Peanuts, Shellfish, Strawberries, Tomatoes, Chocolate, Pollen, Cats)
}``````