Skip to content

Instantly share code, notes, and snippets.

@will
Created December 7, 2010 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save will/731409 to your computer and use it in GitHub Desktop.
Save will/731409 to your computer and use it in GitHub Desktop.
null pointer exception
; no more null pointer :)
(use 'clojure.set)
(def tens (range 1 10))
(def ones (range 0 10))
(defn combine [a b] (+ (* 10 a) b))
(defn uniq [a b n] (if (= a n) b a))
(defn safed [a b] (if (zero? b) 0 (/ a b)))
(def answers
(for [a tens b ones c tens d ones
:when (let [
inter (intersection (set [a b]) (set [c d]))
inter-int (first inter)
ab (combine a b)
cd (combine c d) ]
(and
(= 1 (count inter))
(< (safed ab cd) 1)
(not (= 0 b d))
(= (safed ab cd) (safed (uniq a b inter-int) (uniq c d inter-int)))
))]
[(combine a b) (combine c d)]))
(prn answers)
(prn (map #(/ (first %) (last %)) answers))
(prn (reduce * (map #(/ (first %) (last %)) answers)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment