Medication contraindications
You probably know this, but here's some background so we're all on the same page. Some medicines shouldn't be taken in combination. When two medications shouldn't be taken together, it's called a contraindication.
(def patient-medications [{:name "Blythenal"
:rxNorm "blyth"}
{:name "Masbutol"
:rxNorm "masbut"}
{:name "Welbutril"
:rxNorm "welb"}])
(def contraindication-pairs [["nr913ng" "blyth"]
["masbut" "87f2h139049f"]
["nr913ng" "j1j81f0"]
["blyth" "welb"]
["masbut" "welb"]])
Your task is to take the list of medications a patient has and a list of contraindication pairs, and determine what pairs of medications (if any) they are prescribed that don't mix well.
;; complete this function
(defn contraindications [meds pairs]
It should return nil
if there are no contraindications and a collection of the contraindications (pairs) if there are any.
Bonus: Make it a linear algorithm (linear in the number of the patient medications and the number of the contraindication pairs).
Email submissions to eric@purelyfunctional.tv until June 8, 2020. You can discuss the submissions in the comments below.
@ndonolli, that's an interesting comment - traversing two structures is still linear time indeed. The use of quadratic times pair generation of meds' rxNorm values might also be worthwhile when the meds list is much shorter than the contra pairs, if the inner pairs were sets instead of vectors.
Suppose meds is a 10 rxNorm list, and pairs is 10,000 long. As you say, if set membership is a constant time lookup, then quadratic-time generation of 10x10 = 100 meds combo duples to be checked for membership in 10000 pairs set looks good. However, the 10,000 pairs needing to be converted to sets with something like
(set (map set pairs))
prior, that is probably still slower than straight linear operation over the pairs.EDIT. Actually, we wouldn't need to make every contra pair a set, just put the 10K pairs in a set:
(set pairs)
since the 100 med pairs have all orderings. I don't know how much does the conversion from vector to set cost, but it's at most linear then probably worth it: