Avatar of brushmate

brushmate's solution

to Allergies in the Kotlin Track

Published at Oct 14 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.

Setup

Go through the setup instructions for Kotlin to install the necessary dependencies:

https://exercism.io/tracks/kotlin/installation

Making the test suite pass

Execute the tests with:

$ gradlew test

Use gradlew.bat if you're on Windows

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by removing the @Ignore annotation.

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

import org.junit.Test
import org.junit.Ignore
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue

class AllergiesTest {

    @Test
    fun noAllergiesMeansNotAllergicToAnything() {
        val allergies = Allergies(0)

        assertFalse(allergies.isAllergicTo(Allergen.PEANUTS))
        assertFalse(allergies.isAllergicTo(Allergen.CATS))
        assertFalse(allergies.isAllergicTo(Allergen.STRAWBERRIES))
    }

    @Ignore
    @Test
    fun allergicToEggs() {
        val allergies = Allergies(1)

        assertTrue(allergies.isAllergicTo(Allergen.EGGS))
    }

    @Ignore
    @Test
    fun allergicToEggsAndOtherThings() {
        val allergies = Allergies(5)

        assertTrue(allergies.isAllergicTo(Allergen.EGGS))
        assertTrue(allergies.isAllergicTo(Allergen.SHELLFISH))
        assertFalse(allergies.isAllergicTo(Allergen.STRAWBERRIES))
    }

    @Ignore
    @Test
    fun noAllergies() {
        val allergies = Allergies(0)

        assertTrue(allergies.getList().isEmpty())
    }

    @Ignore
    @Test
    fun allergicToJustEggs() {
        val allergies = Allergies(1)
        val expectedAllergens = listOf(Allergen.EGGS)

        assertEquals(expectedAllergens, allergies.getList())
    }

    @Ignore
    @Test
    fun allergicToJustPeanuts() {
        val allergies = Allergies(2)
        val expectedAllergens = listOf(Allergen.PEANUTS)

        assertEquals(expectedAllergens, allergies.getList())
    }

    @Ignore
    @Test
    fun allergicToJustStrawberries() {
        val allergies = Allergies(8)
        val expectedAllergens = listOf(Allergen.STRAWBERRIES)

        assertEquals(expectedAllergens, allergies.getList())
    }

    @Ignore
    @Test
    fun allergicToEggsAndPeanuts() {
        val allergies = Allergies(3)
        val expectedAllergens = listOf(
                Allergen.EGGS,
                Allergen.PEANUTS
        )

        assertEquals(expectedAllergens, allergies.getList())
    }

    @Ignore
    @Test
    fun allergicToEggsAndShellfish() {
        val allergies = Allergies(5)
        val expectedAllergens = listOf(
                Allergen.EGGS,
                Allergen.SHELLFISH
        )

        assertEquals(expectedAllergens, allergies.getList())
    }

    @Ignore
    @Test
    fun allergicToLotsOfStuff() {
        val allergies = Allergies(248)
        val expectedAllergens = listOf(
                Allergen.STRAWBERRIES,
                Allergen.TOMATOES,
                Allergen.CHOCOLATE,
                Allergen.POLLEN,
                Allergen.CATS
        )

        assertEquals(expectedAllergens, allergies.getList())
    }

    @Ignore
    @Test
    fun allergicToEverything() {
        val allergies = Allergies(255)
        val expectedAllergens = listOf(
                Allergen.EGGS,
                Allergen.PEANUTS,
                Allergen.SHELLFISH,
                Allergen.STRAWBERRIES,
                Allergen.TOMATOES,
                Allergen.CHOCOLATE,
                Allergen.POLLEN,
                Allergen.CATS
        )

        assertEquals(expectedAllergens, allergies.getList())
    }

    @Ignore
    @Test
    fun ignoreNonAllergenScoreParts() {
        val allergies = Allergies(509)
        val expectedAllergens = listOf(
                Allergen.EGGS,
                Allergen.SHELLFISH,
                Allergen.STRAWBERRIES,
                Allergen.TOMATOES,
                Allergen.CHOCOLATE,
                Allergen.POLLEN,
                Allergen.CATS
        )

        assertEquals(expectedAllergens, allergies.getList())
    }

}
class Allergies(private val code: Int) {

    fun isAllergicTo(allergen: Allergen): Boolean = code and allergen.score != 0

    fun getList(): List<Allergen> = Allergen.values().filter { isAllergicTo(it) }

}

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?