Avatar of jf-parent

jf-parent's solution

to Allergies in the Clojure Track

Published at Nov 23 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)

(def allergies-list [:eggs :peanuts :shellfish :strawberries :tomatoes :chocolate :pollen :cats])

(defn expt [x n]
  (reduce * (repeat n x)))

(defn log2 [n]
  (/ (Math/log n) (Math/log 2)))

(defn allergies [score]
  (if (<= score 0)
    []
    (filter
     #(not (nil? %))
     (loop [result '()
            index (int (Math/floor (log2 score)))
            current-score score]
       (let [power2 (expt 2 index)]
         (if (or (< index 0) (zero? current-score))
           result
           (if (>= (- current-score power2) 0)
             (recur (conj result (nth allergies-list index nil)) (dec index) (- current-score power2))
             (recur result (dec index) current-score))))))))

(defn allergic-to? [score allergie]
  (some #{allergie} (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?