Avatar of fyrchik

fyrchik's solution

to Allergies in the Clojure Track

Published at Oct 13 2020 · 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.

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

(ns allergies-test
  (:require [clojure.test :refer [deftest is]]
            allergies))

(deftest no-allergies-at-all
  (is (= [] (allergies/allergies 0))))

(deftest allergic-to-just-eggs
  (is (= [:eggs] (allergies/allergies 1))))

(deftest allergic-to-just-peanuts
  (is (= [:peanuts] (allergies/allergies 2))))

(deftest allergic-to-just-strawberries
  (is (= [:strawberries] (allergies/allergies 8))))

(deftest allergic-to-eggs-and-peanuts
  (is (= [:eggs :peanuts] (allergies/allergies 3))))

(deftest allergic-to-more-than-eggs-but-not-peanuts
  (is (= [:eggs :shellfish] (allergies/allergies 5))))

(deftest allergic-to-lots-of-stuff
  (is (= [:strawberries :tomatoes :chocolate :pollen :cats]
         (allergies/allergies 248))))

(deftest allergic-to-everything
  (is (= [:eggs :peanuts :shellfish :strawberries
          :tomatoes :chocolate :pollen :cats]
         (allergies/allergies 255))))

(deftest no-allergies-means-not-allergic
  (is (not (allergies/allergic-to? 0 :peanuts)))
  (is (not (allergies/allergic-to? 0 :cats)))
  (is (not (allergies/allergic-to? 0 :strawberries))))

(deftest is-allergic-to-eggs
  (is (allergies/allergic-to? 1 :eggs)))

(deftest allergic-to-eggs-in-addition-to-other-stuff
  (is (allergies/allergic-to? 5 :eggs)))

(deftest ignore-non-allergen-score-parts
  (is (= [:eggs :shellfish :strawberries :tomatoes :chocolate :pollen :cats]
         (allergies/allergies 509))))
(ns allergies)

(defn allergies [n]
  (cond-> []
    (bit-test n 0) (conj :eggs)
    (bit-test n 1) (conj :peanuts)
    (bit-test n 2) (conj :shellfish)
    (bit-test n 3) (conj :strawberries)
    (bit-test n 4) (conj :tomatoes)
    (bit-test n 5) (conj :chocolate)
    (bit-test n 6) (conj :pollen)
    (bit-test n 7) (conj :cats))
)

(defn allergic-to? [n allergen]
  (contains? (set (allergies n)) allergen)
)

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?