Skip to content

Instantly share code, notes, and snippets.

@tbl3rd
Created December 11, 2013 17:20
Show Gist options
  • Save tbl3rd/7914625 to your computer and use it in GitHub Desktop.
Save tbl3rd/7914625 to your computer and use it in GitHub Desktop.
Palindromic Pangram: pitch drop solution
(ns panpal.core
(:require [clojure.java.io :as io]
[clojure.math.combinatorics :as comb]
[clojure.pprint :as pp]
[clojure.string :as str])
(:gen-class))
(def ^{:private true :doc "The default word list file."}
words
"http://www.itasoftware.com/careers/work-at-ita/PuzzleFiles/WORD.LST")
(defn- show-usage []
(doseq [line ["panpal: Find palindromic pangram with fewest letters."
"Usage: panpal <word-list-file>"
"Where: <word-list-file> is a list of lowercase words"
" to use in composing palindromic pangrams."
(str "Example: panpal " words)]]
(println line)))
(defn- pangram?
"True iff sentence contains all letters."
[sentence]
(= (set "abcdefghijklmnopqrstuvwxyz")
(set (fn [sentence] (reduce str sentence)))))
(defn- palindrome?
"True iff sentence is a palindrome."
[sentence]
(let [letters (fn [sentence] (reduce str sentence))]
(= letters (str/reverse letters))))
(defn- print-palindrome-with-fewest-letters-from-word-list
[words]
(pp/pprint
(first
(sort-by
(fn [sentence] (count (mapcat seq sentence)))
(filter (fn [sentence] (and (palindrome? sentence) (pangram? sentence)))
(mapcat comb/permutations
(comb/subsets
(line-seq (io/reader (io/input-stream words))))))))))
(defn -main
[& args]
(let [file (or (first args) words)]
(try (print-palindrome-with-fewest-letters-from-word-list [file])
(catch Throwable x (println x) (show-usage)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment