Skip to content

Instantly share code, notes, and snippets.

@tonsky

tonsky/CIS194_card.clj

Last active Feb 13, 2017
Embed
What would you like to do?
(defn digits [x]
(loop [r x, digits []]
(if (== 0 r)
digits
(recur (quot r 10) (conj digits (rem r 10))))))
(defn double-odd-digits [digits]
(map-indexed (fn [i d] (if (odd? i) (* 2 d) d)) digits))
(defn valid? [x]
(let [sum (->> (digits x)
(double-odd-digits)
(mapcat digits)
(reduce + 0))]
(== 0 (rem sum 10))))
(use 'clojure.test)
(is (valid? 4012888888881881))
(is (not (valid? 4012888888881882)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.