Avatar of muguadev

muguadev's solution

to Allergies in the Clojure Track

Published at Nov 02 2020 · 0 comments
Test suite

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.


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.


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

(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)

(defmacro filter-by-index [pred coll]
  `(filter (fn [[idx# val#]] (~pred idx#)) (map-indexed (fn [i# v#] [i# v#]) ~coll)))

(defn selected [score idx] 
  (pos? (bit-and score (int (Math/pow 2 idx)))))

(def ALLERGENS [:eggs :peanuts :shellfish :strawberries :tomatoes :chocolate :pollen :cats])

(defn allergies [score]
  (mapv last (filter-by-index (partial selected score) ALLERGENS)))

(defn allergic-to? [score allergen]
  (some? (some #{allergen} (allergies score))))

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?