Skip to content

Instantly share code, notes, and snippets.

@jeremyrsellars
Created June 3, 2014 02:52
Show Gist options
  • Save jeremyrsellars/2ccb78d2b5c3bb092683 to your computer and use it in GitHub Desktop.
Save jeremyrsellars/2ccb78d2b5c3bb092683 to your computer and use it in GitHub Desktop.
Pick the +/- operator to put between each digit from 1 to N
(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