Created
October 4, 2013 12:44
-
-
Save jukworks/6825357 to your computer and use it in GitHub Desktop.
ROT-13 encryption in Clojure
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
(defn shift13 [start n] | |
(let [s (int start) | |
c (int n)] | |
(char (if (<= s c (+ s 25)) | |
(+ s (mod (+ (- c s) 13) 26)) | |
n)))) | |
(defn rot13 [st] | |
(->> (map (fn [x] | |
(let [n (int x)] | |
(cond | |
(<= 65 n 90) (shift13 \A x) | |
(<= 97 n 122) (shift13 \a x) | |
:else x))) | |
(seq st)) | |
(apply str))) | |
;; (rot13 "hEllo") => "uRyyb" | |
;; (rot13 "uRyyb") => "hEllo" | |
;; (rot13 "The quick brown fox jumps over the lazy dog.") => "Gur dhvpx oebja sbk whzcf bire gur ynml qbt." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment