🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of pfertyk

pfertyk's solution

to Allergies in the Python Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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.

Exception messages

Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include a message.

To raise a message with an exception, just write it as an argument to the exception type. For example, instead of raise Exception, you should write:

raise Exception("Meaningful message indicating the source of the error")

Running the tests

To run the tests, run the appropriate command below (why they are different):

  • Python 2.7: py.test allergies_test.py
  • Python 3.4+: pytest allergies_test.py

Alternatively, you can tell Python to run the pytest module (allowing the same command to be used regardless of Python version): python -m pytest allergies_test.py

Common pytest options

  • -v : enable verbose output
  • -x : stop running tests on first failure
  • --ff : run failures from previous test before running other test cases

For other options, see python -m pytest -h

Submitting Exercises

Note that, when trying to submit an exercise, make sure the solution is in the $EXERCISM_WORKSPACE/python/allergies directory.

You can find your Exercism workspace by running exercism debug and looking for the line that starts with Workspace.

For more detailed information about running tests, code style and linting, please 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.

allergies_test.py

import unittest

from allergies import Allergies

# Python 2/3 compatibility
if not hasattr(unittest.TestCase, 'assertCountEqual'):
    unittest.TestCase.assertCountEqual = unittest.TestCase.assertItemsEqual


# Tests adapted from `problem-specifications//canonical-data.json` @ v1.1.0

class AllergiesTest(unittest.TestCase):
    def test_no_allergies_means_not_allergic(self):
        allergies = Allergies(0)
        self.assertIs(allergies.is_allergic_to('peanuts'), False)
        self.assertIs(allergies.is_allergic_to('cats'), False)
        self.assertIs(allergies.is_allergic_to('strawberries'), False)

    def test_is_allergic_to_eggs(self):
        self.assertIs(Allergies(1).is_allergic_to('eggs'), True)

    def test_allergic_to_eggs_in_addition_to_other_stuff(self):
        allergies = Allergies(5)
        self.assertIs(allergies.is_allergic_to('eggs'), True)
        self.assertIs(allergies.is_allergic_to('shellfish'), True)
        self.assertIs(allergies.is_allergic_to('strawberries'), False)

    def test_no_allergies_at_all(self):
        self.assertEqual(Allergies(0).lst, [])

    def test_allergic_to_just_eggs(self):
        self.assertEqual(Allergies(1).lst, ['eggs'])

    def test_allergic_to_just_peanuts(self):
        self.assertEqual(Allergies(2).lst, ['peanuts'])

    def test_allergic_to_just_strawberries(self):
        self.assertEqual(Allergies(8).lst, ['strawberries'])

    def test_allergic_to_eggs_and_peanuts(self):
        self.assertCountEqual(Allergies(3).lst, ['eggs', 'peanuts'])

    def test_allergic_to_more_than_eggs_but_not_peanuts(self):
        self.assertCountEqual(Allergies(5).lst, ['eggs', 'shellfish'])

    def test_allergic_to_lots_of_stuff(self):
        self.assertCountEqual(
            Allergies(248).lst,
            ['strawberries', 'tomatoes', 'chocolate', 'pollen', 'cats'])

    def test_allergic_to_everything(self):
        self.assertCountEqual(
            Allergies(255).lst, [
                'eggs', 'peanuts', 'shellfish', 'strawberries', 'tomatoes',
                'chocolate', 'pollen', 'cats'
            ])

    def test_ignore_non_allergen_score_parts_only_eggs(self):
        self.assertEqual(Allergies(257).lst, ['eggs'])

    def test_ignore_non_allergen_score_parts(self):
        self.assertCountEqual(
            Allergies(509).lst, [
                'eggs', 'shellfish', 'strawberries', 'tomatoes', 'chocolate',
                'pollen', 'cats'
            ])


if __name__ == '__main__':
    unittest.main()
from itertools import compress


class Allergies:
    all = ['eggs', 'peanuts', 'shellfish', 'strawberries', 'tomatoes', 'chocolate', 'pollen', 'cats']
    def __init__(self, score):
        bitmask = (int(i) for i in reversed('{0:08b}'.format(score)))
        self.lst = list(compress(Allergies.all, bitmask))

    def is_allergic_to(self, allergen):
        return allergen in self.lst

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?