Skip to content

Instantly share code, notes, and snippets.

@Raynes
Created November 7, 2009 02:14
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 Raynes/228482 to your computer and use it in GitHub Desktop.
Save Raynes/228482 to your computer and use it in GitHub Desktop.
(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