Last active
October 18, 2019 04:13
-
-
Save lumie1337/4a6a93b71976cb7c3f9fef17a1d2c10f to your computer and use it in GitHub Desktop.
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
(defn- valid-pattern [s] (boolean (re-matches #"\d-\d{3}-\d{5}-[\dX]" s))) | |
(defn- valid-checksum [s] | |
(let [csum (->> s | |
(map {\0 0 \1 1 \2 2 \3 3 \4 4 \5 5 \6 6 \7 7 \8 8 \9 9 \X 10}) | |
(keep identity) | |
(map * (range 10 0 -1)) | |
(reduce +))] | |
(zero? (rem csum 11)))) | |
(def isbn? (every-pred valid-pattern valid-checksum)) |
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 twelve | |
(:require [clojure.string :as str])) | |
;; lazy interpose with a special last separator | |
(defn interpose-last | |
[sep sep-last coll] | |
(if (next coll) | |
`(~@(interpose sep (butlast coll)) ~sep-last ~(last coll)) | |
coll)) | |
;; data | |
(def data | |
{:sts ["first" "second" "third" "fourth" "fifth" "sixth" "seventh" "eighth" "ninth" "tenth" "eleventh" "twelfth"] | |
:verses ["a Partridge in a Pear Tree" | |
"two Turtle Doves" | |
"three French Hens" | |
"four Calling Birds" | |
"five Gold Rings" | |
"six Geese-a-Laying" | |
"seven Swans-a-Swimming" | |
"eight Maids-a-Milking" | |
"nine Ladies Dancing" | |
"ten Lords-a-Leaping" | |
"eleven Pipers Piping" | |
"twelve Drummers Drumming"]}) | |
;; recite one verse | |
(defn recite1 [verse] | |
`("On the " | |
~((:sts data) (dec verse)) | |
" day of Christmas my true love gave to me: " | |
~@(->> (range (dec verse) -1 -1) | |
(map (:verses data)) | |
(interpose-last ", " ", and ")) | |
".")) | |
;; recite verses from start to end | |
(defn recite [start end] | |
(->> (range start (inc end)) | |
(map recite1) | |
(interpose "\n\n") | |
(apply concat) | |
(apply str))) | |
(recite 1 12) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment