Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of rootulp

rootulp's solution

to Allergies in the Ruby 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.


For installation and learning resources, refer to the exercism help 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.0.0 879bc89
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_no_allergies_at_all
    skip
    allergies = Allergies.new(0)
    assert_equal %w(), allergies.list
  end

  def test_allergic_to_just_eggs
    skip
    allergies = Allergies.new(1)
    assert_equal %w(eggs), allergies.list
  end

  def test_allergic_to_just_peanuts
    skip
    allergies = Allergies.new(2)
    assert_equal %w(peanuts), allergies.list
  end

  def test_allergic_to_just_strawberries
    skip
    allergies = Allergies.new(8)
    assert_equal %w(strawberries), allergies.list
  end

  def test_allergic_to_eggs_and_peanuts
    skip
    allergies = Allergies.new(3)
    assert_equal %w(eggs peanuts), allergies.list
  end

  def test_allergic_to_more_than_eggs_but_not_peanuts
    skip
    allergies = Allergies.new(5)
    assert_equal %w(eggs shellfish), allergies.list
  end

  def test_allergic_to_lots_of_stuff
    skip
    allergies = Allergies.new(248)
    assert_equal %w(strawberries tomatoes chocolate pollen cats), allergies.list
  end

  def test_allergic_to_everything
    skip
    allergies = Allergies.new(255)
    assert_equal %w(eggs peanuts shellfish strawberries tomatoes chocolate pollen cats), allergies.list
  end

  def test_ignore_non_allergen_score_parts
    skip
    allergies = Allergies.new(509)
    assert_equal %w(eggs shellfish strawberries tomatoes chocolate pollen cats), allergies.list
  end

  # Problems in exercism evolve over time, as we find better ways to ask
  # questions.
  # The version number refers to the version of the problem you solved,
  # not your solution.
  #
  # Define a constant named VERSION inside of the top level BookKeeping
  # module, which may be placed near the end of your file.
  #
  # In your file, it will look like this:
  #
  # module BookKeeping
  #   VERSION = 1 # Where the version number matches the one in the test.
  # end
  #
  # If you are curious, read more about constants on RubyDoc:
  # http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html

  def test_bookkeeping
    skip
    assert_equal 1, BookKeeping::VERSION
  end
end
# Solution essentially taken from:
# https://github.com/xavier/exercism-assignments/blob/master/ruby/allergies/allergies.rb
class Allergies
  VALUES = {
    'eggs'         => 1,
    'peanuts'      => 2,
    'shellfish'    => 4,
    'strawberries' => 8,
    'tomatoes'     => 16,
    'chocolate'    => 32,
    'pollen'       => 64,
    'cats'         => 128
  }.freeze

  attr_reader :score
  def initialize(score)
    @score = score
  end

  def allergic_to?(item)
    VALUES[item] & score != 0
  end

  def list
    VALUES.keys.select { |item| allergic_to?(item) }
  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?