Created
November 7, 2009 02:14
-
-
Save Raynes/228482 to your computer and use it in GitHub Desktop.
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 cipher) | |
(def alphabet-nums (apply hash-map (interleave (range 1 27) "abcdefghijklmnopqrstuvwxyz"))) | |
(def alphabet-chars (apply hash-map (interleave "abcdefghijklmnopqrstuvwxyz" (range 1 27)))) | |
(defn is-letter? [s] | |
(if (re-find #"(?i)[a-z]" (.toString s)) true false)) | |
(defn shift-char | |
"Shifts the given Character down the alphabet by three." | |
[char] | |
(let [num (+ char 3) | |
index (if (and (> num 25) (not= num 26)) (- num 26) num)] | |
(alphabet-nums index))) | |
(defn trans-string | |
"Transforms a string into a vector of numbers | |
that correspond with their characters." | |
[s] | |
(for [x s] (alphabet-chars x))) | |
(defn cipher | |
"Encrypts a string with Caesar's cipher." | |
[s] | |
(apply str | |
(for [x (trans-string (.toLowerCase (apply str (filter is-letter? s))))] | |
(shift-char x)))) | |
(defn to-cleartext | |
"Transforms ciphertext into cleartext." | |
[s]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment