Created
June 3, 2014 02:52
-
-
Save jeremyrsellars/2ccb78d2b5c3bb092683 to your computer and use it in GitHub Desktop.
Pick the +/- operator to put between each digit from 1 to N
This file contains 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
(defn zero-sum-game [n] | |
(let [explode | |
(fn explode [old item] | |
(concat (map #(cons item %) old) | |
(map #(cons (- item) %) old))) | |
candidate-combinations | |
(->> (range 2 (inc n)) | |
(reduce explode ['(1)])) | |
sum-is-zero? | |
(fn sum-is-zero? [a-seq-of-numbers] (zero? (apply + a-seq-of-numbers))) | |
zero-combinations | |
(filter sum-is-zero? candidate-combinations) | |
num->string | |
#(if (< 1 %) (str \+ %) (str %)) | |
seq->string | |
(fn seq->string [a-seq] | |
(->> a-seq | |
(map num->string) | |
reverse | |
(apply str) | |
#_(interpose " ") | |
(apply str))) | |
nice-strings | |
(map seq->string zero-combinations)] | |
nice-strings)) | |
(zero-sum-game 7) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment