Skip to content

Instantly share code, notes, and snippets.

@dustinbrownman
Last active December 22, 2022 04:38
Show Gist options
  • Select an option

  • Save dustinbrownman/6382194 to your computer and use it in GitHub Desktop.

Select an option

Save dustinbrownman/6382194 to your computer and use it in GitHub Desktop.
Given a person's allergy score, it can tell if that person is allergic to a certain item and generate a list of all that persons allergies. Here's a list of allergens covered with their scores: eggs (1) peanuts (2) shellfish (4) strawberries (8) tomatoes (16) chocolate (32) pollen (64) cats (128)
class Allergies
ALLERGENS = {
128 => "cats",
64 => "pollen",
32 => "chocolate",
16 => "tomatoes",
8 => "strawberries",
4 => "shellfish",
2 => "peanuts",
1 => "eggs"
}
def initialize(allergy_score)
@list = []
parse_score(allergy_score)
end
def list
@list
end
def parse_score(allergy_score)
ALLERGENS.each do |score, allergen|
if allergy_score >= score
@list << allergen
allergy_score -= score
end
end
end
def is_alergic?(allergen)
@list.include?(allergen)
end
end
require "rspec"
require "allergies"
describe 'Allergies' do
describe 'initialize' do
it 'does not generate a list form a score of zero' do
allergies = Allergies.new(0)
allergies.list.should eq []
end
end
describe 'parse_score' do
it 'returns "eggs" for a score of 1' do
allergies = Allergies.new(1)
allergies.list.should eq ["eggs"]
end
it 'returns "peanuts" and "chocolate" for a score of 34' do
allergies = Allergies.new(34)
allergies.list.should eq ["chocolate", "peanuts"]
end
it 'returns everything for a score of 255' do
allergies = Allergies.new(255)
allergies.list.should eq ["cats", "pollen", "chocolate", "tomatoes", "strawberries", "shellfish", "peanuts", "eggs"]
end
end
describe 'is_alergic?' do
it 'is true for a person with a score of 64 being allergic to pollen' do
allergies = Allergies.new(64)
allergies.is_alergic?("pollen").should be_true
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment