Created
April 14, 2015 01:06
-
-
Save orb/c3a8031963ed07a89449 to your computer and use it in GitHub Desktop.
Austin Clojure 4/13 alphabet-cipher
This file contains 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 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