Permalink
Browse files

Add Countdown.clj and update the wordgen.clj because assoc can take a

default value.
  • Loading branch information...
1 parent 6243cdb commit 507c064f7264d93f6f52913c7a496898c03fad16 @fffej committed Feb 17, 2009
Showing with 41 additions and 4 deletions.
  1. +39 −0 countdown.clj
  2. +2 −4 wordgen.clj
View
@@ -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)))))
+
View
@@ -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]

0 comments on commit 507c064

Please sign in to comment.