Skip to content

Commit

Permalink
Add Countdown.clj and update the wordgen.clj because assoc can take a
Browse files Browse the repository at this point in the history
default value.
  • Loading branch information
fffej committed Feb 17, 2009
1 parent 6243cdb commit 507c064
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
39 changes: 39 additions & 0 deletions countdown.clj
@@ -0,0 +1,39 @@
;;; Solving Countdown by Bruteforce
;;; jeff.foster@acm.org

(ns countdown
(:use clojure.contrib.combinatorics)
(:use clojure.contrib.seq-utils))

(def *operators* [+ - / *])

(defn is-valid [op a b]
(cond
(= '+ op) true
(= '- op) (> a b)
(= '* op) true
(= '/ op) (= 0 (mod a b))))

(defn expr
"A list of expressions for a and b"
[a b]
(map (fn [x] (x a b)) *operators*))

(defn expressions-helper
"Given a lst, build up all valid Countdown expressions"
[x]
(cond
(< (count x) 2) x
(= 2 (count x)) (apply expr x)
:else
(let [exps (apply expr (take 2 x))
remd (drop 2 x)]
(mapcat expressions-helper (map (fn [x] (cons x remd)) exps)))))

(defn expressions [lst]
(if (nil? lst)
nil
(lazy-cat
(mapcat expressions-helper (permutations lst))
(expressions (rest lst)))))

6 changes: 2 additions & 4 deletions wordgen.clj
Expand Up @@ -10,10 +10,8 @@
(let [w1 (first v) w2 (second v) val (get accum w1)]
(if (nil? val)
(assoc accum w1 {w2 1})
(let [currentVal (get val w2)]
(if (nil? currentVal)
(assoc accum w1 (conj val {w2 1}))
(assoc accum w1 (conj val {w2 (inc currentVal)})))))))
(let [currentVal (get val w2 0)]
(assoc accum w1 (conj val {w2 (inc currentVal)}))))))
{} word-pairs)))

(defn frequency-map-count [m word]
Expand Down

0 comments on commit 507c064

Please sign in to comment.