Avatar of w1zeman1p

w1zeman1p's solution

to Allergies in the Ruby Track

Published at Feb 18 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.


For installation and learning resources, refer to the Ruby resources page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

gem install minitest

If you would like color output, you can require 'minitest/pride' in the test file, or note the alternative instruction, below, for running the test file.

Run the tests from the exercise directory using the following command:

ruby allergies_test.rb

To include color from the command line:

ruby -r minitest/pride allergies_test.rb

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

require 'minitest/autorun'
require_relative 'allergies'

# Common test data version: 1.2.0 17a2ab2
class AllergiesTest < Minitest::Test
  def test_no_allergies_means_not_allergic
    # skip
    allergies = Allergies.new(0)
    refute allergies.allergic_to?('peanuts')
    refute allergies.allergic_to?('cats')
    refute allergies.allergic_to?('strawberries')
  end

  def test_is_allergic_to_eggs
    skip
    allergies = Allergies.new(1)
    assert allergies.allergic_to?('eggs')
  end

  def test_allergic_to_eggs_in_addition_to_other_stuff
    skip
    allergies = Allergies.new(5)
    assert allergies.allergic_to?('eggs')
    assert allergies.allergic_to?('shellfish')
    refute allergies.allergic_to?('strawberries')
  end

  def test_allergic_to_strawberries_but_not_peanuts
    skip
    allergies = Allergies.new(9)
    assert allergies.allergic_to?('eggs')
    refute allergies.allergic_to?('peanuts')
    refute allergies.allergic_to?('shellfish')
    assert allergies.allergic_to?('strawberries')
  end

  def test_no_allergies_at_all
    skip
    allergies = Allergies.new(0)
    expected_items = []
    assert_equal expected_items, allergies.list.sort
  end

  def test_allergic_to_just_eggs
    skip
    allergies = Allergies.new(1)
    expected_items = ["eggs"]
    assert_equal expected_items, allergies.list.sort
  end

  def test_allergic_to_just_peanuts
    skip
    allergies = Allergies.new(2)
    expected_items = ["peanuts"]
    assert_equal expected_items, allergies.list.sort
  end

  def test_allergic_to_just_strawberries
    skip
    allergies = Allergies.new(8)
    expected_items = ["strawberries"]
    assert_equal expected_items, allergies.list.sort
  end

  def test_allergic_to_eggs_and_peanuts
    skip
    allergies = Allergies.new(3)
    expected_items = ["eggs", "peanuts"]
    assert_equal expected_items, allergies.list.sort
  end

  def test_allergic_to_more_than_eggs_but_not_peanuts
    skip
    allergies = Allergies.new(5)
    expected_items = ["eggs", "shellfish"]
    assert_equal expected_items, allergies.list.sort
  end

  def test_allergic_to_lots_of_stuff
    skip
    allergies = Allergies.new(248)
    expected_items = ["cats", "chocolate", "pollen", "strawberries", "tomatoes"]
    assert_equal expected_items, allergies.list.sort
  end

  def test_allergic_to_everything
    skip
    allergies = Allergies.new(255)
    expected_items = ["cats", "chocolate", "eggs", "peanuts", "pollen", "shellfish", "strawberries", "tomatoes"]
    assert_equal expected_items, allergies.list.sort
  end

  def test_ignore_non_allergen_score_parts
    skip
    allergies = Allergies.new(509)
    expected_items = ["cats", "chocolate", "eggs", "pollen", "shellfish", "strawberries", "tomatoes"]
    assert_equal expected_items, allergies.list.sort
  end
end
class Allergies
  ALLERGENS = {
    "eggs" => 1,
    "peanuts" => 2,
    "shellfish" => 4,
    "strawberries" => 8,
    "tomatoes" => 16,
    "chocolate" => 32,
    "pollen" => 64,
    "cats" => 128,
  }

  def initialize(score)
    @score = score
  end

  def allergic_to?(allergen)
    ALLERGENS[allergen] & @score > 0
  end

  def list
    ALLERGENS.keys.select(&method(:allergic_to?))
  end
end

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?