Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Countdown.clj and update the wordgen.clj because assoc can take a
default value.
- Loading branch information
Showing
2 changed files
with
41 additions
and
4 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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))))) | ||
|
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