Created
December 27, 2010 18:12
-
-
Save folsen/756374 to your computer and use it in GitHub Desktop.
A word-problem game from a swedish newpaper solved in clojure
This file contains hidden or 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
(ns svd.core | |
(:use | |
clojure.contrib.combinatorics) | |
(:require | |
[clojure.contrib.duck-streams :as ds] | |
[clojure.contrib.str-utils :as str])) | |
(defn wordlist [] | |
(set (map #(re-find #"[^/]*" %) (ds/read-lines "sv_se.dic")))) | |
(defn get-all-words [letters required] | |
(let [perms (map #(apply str %) | |
(filter #(contains? (into #{} %) required) | |
(mapcat permutations | |
(mapcat #(combinations letters %) | |
'(4 5 6 7 8 9))))) | |
words (wordlist)] | |
(filter #(contains? words %) perms))) | |
(defn main [letters required] | |
(let [words (get-all-words | |
(into #{} letters) | |
(. required charAt 0))] | |
(println (str "Hittade " (count words) " ord.")) | |
(loop [n 1] | |
(if (not (> n (count words))) | |
(do | |
(println (str n ". " (nth (sort-by #(count %) words) (- n 1)))) | |
(recur (+ n 1))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment