Skip to content

Instantly share code, notes, and snippets.

@pstephens
Last active August 29, 2015 14:19
Show Gist options
  • Save pstephens/4edc5f68fd24173f5d5c to your computer and use it in GitHub Desktop.
Save pstephens/4edc5f68fd24173f5d5c to your computer and use it in GitHub Desktop.
Alphabet Cipher solution
;; Solution to https://github.com/gigasquid/wonderland-clojure-katas/tree/master/alphabet-cipher
(ns alphabet-cipher.coder)
(defn crypt [op keyword message]
(letfn [
(tochar [idx] (char (+ idx (int \a))))
(toidx [ch] (- (int ch) (int \a)))
(encode [keychar msgchar] (tochar (mod (op (toidx msgchar) (toidx keychar)) 26)))]
(->> message
(map encode (cycle keyword))
(apply str))))
(defn encode [keyword message] (crypt + keyword message))
(defn decode [keyword message] (crypt - keyword message))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment