Skip to content

Instantly share code, notes, and snippets.

@jsanders
Created May 7, 2013 21:24
Show Gist options
  • Save jsanders/5536260 to your computer and use it in GitHub Desktop.
Save jsanders/5536260 to your computer and use it in GitHub Desktop.
Clojure subsets
(use 'clojure.contrib.combinatorics)
(def small-numbers [ 1 2 3 4 6 ])
(def large-numbers [ 3 4 9 14 15 19 28 37 47 50 54 56 59 61 70 73 78 81 92 95 97 99 ])
(defn largest-sum-of-rest?
[ col ]
(let [ [ largest & others ] (reverse (sort col)) ]
(= largest (reduce + others))))
(defn size
[ col ]
(reduce (fn [x _] (inc x)) 0 col))
(defn num-subsets-with-largest-sum-of-rest
[ col ]
(size (filter largest-sum-of-rest? (clojure.contrib.combinatorics/subsets col))))
(println (num-subsets-with-largest-sum-of-rest large-numbers))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment