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:
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:
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
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]]
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)))
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.
Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments