Skip to content

Instantly share code, notes, and snippets.

@wofockham
Created April 8, 2014 23:02
Show Gist options
  • Save wofockham/5a22e6aadb568a3c8e20 to your computer and use it in GitHub Desktop.
Save wofockham/5a22e6aadb568a3c8e20 to your computer and use it in GitHub Desktop.

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.

Write a program that, given a person's score can tell them

a) whether or not they're allergic to a given item, and b) the full list of allergies.

allergies = Allergies.new(34)
allergies.allergic_to?('chocolate')
=> true
allergies.allergic_to?('cats')
=> false
allergies.list
=> ['peanuts', 'chocolate']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment