Created
May 7, 2013 21:24
-
-
Save jsanders/5536260 to your computer and use it in GitHub Desktop.
Clojure subsets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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