6 kyu

Tom's Allergies

150 of 305Lusina

Description:

Allergies

Write a program that, given a person's allergy score, can tell them 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., sorted alphabetically

Example:

>>> allergies = Allergies(0)
>>> allergies.is_allergic_to('peanuts')
False
>>> Allergies(255).allergies()
['cats', 'chocolate', 'eggs', 'peanuts', 'pollen', 'shellfish', 'strawberries', 'tomatoes']
>>> Allergies(259).allergies()
["eggs", "peanuts"]

You will be provided with a class Allergies which will have 2 methods

  • is_allergic_to Checks if Tom is allergic to a particular allergen. Returns True if Tom is allergic, False otherwise

  • allergies Returns a list of what Tom is allergic to. This list must be sorted alphabetically

Must Dos:

Ensure that your function throws a TypeError for invalid inputs such as None(Null), floats, strings, or any data type that is not an integer.

Hint: Use Bitwise ANDing

Algorithms
Puzzles
Fundamentals

Similar Kata:

More By Author:

Check out these other kata created by Lusina

Stats:

CreatedMar 7, 2017
PublishedMar 8, 2017
Warriors Trained920
Total Skips36
Total Code Submissions2079
Total Times Completed305
Python Completions150
Ruby Completions40
JavaScript Completions91
Crystal Completions5
PHP Completions47
Total Stars20
% of votes with a positive feedback rating90% of 113
Total "Very Satisfied" Votes95
Total "Somewhat Satisfied" Votes13
Total "Not Satisfied" Votes5
Total Rank Assessments11
Average Assessed Rank
6 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • Lusina Avatar
  • GiacomoSorbi Avatar
  • smolen Avatar
  • tachyonlabs Avatar
  • hobovsky Avatar
Ad