Skip to content

Instantly share code, notes, and snippets.

@orb
Created April 14, 2015 01:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orb/c3a8031963ed07a89449 to your computer and use it in GitHub Desktop.
Save orb/c3a8031963ed07a89449 to your computer and use it in GitHub Desktop.
Austin Clojure 4/13 alphabet-cipher
(ns alphabet-cipher.coder)
(defn char->num [char]
(- (int char) (int \a)))
(defn num->char [ch]
(char (+ (int ch) (int \a))))
(defn mod26 [n]
(mod n 26))
(defn encode-by [key char]
(-> (char->num char)
(+ (char->num key))
(mod26)
(num->char)))
(defn decode-by [key char]
(-> (char->num char)
(- (char->num key))
(mod26)
(num->char)))
(defn encode [keyword message]
(apply str (map encode-by (cycle keyword) message)))
(defn decode [keyword message]
(apply str (map decode-by (cycle keyword) message)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment