Skip to content

Instantly share code, notes, and snippets.

@cloojure
Created June 1, 2016 01:25
Show Gist options
  • Save cloojure/0726cfad5f27679e19a5dedb65a13dd5 to your computer and use it in GitHub Desktop.
Save cloojure/0726cfad5f27679e19a5dedb65a13dd5 to your computer and use it in GitHub Desktop.
Clojure Rot13 test
(ns tst.clj.core
(:use clj.core
clojure.test
tupelo.core))
(def first-char \a)
(def first-char-val (int first-char))
(defn str->codes
[arg-str]
(mapv int (vec arg-str)))
(defn codes->str
[codes-vec]
(str/join (mapv char codes-vec)))
(defn codes->baseline
[codes-vec]
(mapv #(- % first-char-val) codes-vec))
(defn baseline->codes
[codes-vec]
(mapv #(+ % first-char-val) codes-vec))
(defn rot13
[code-vec]
(mapv (fn [char-code]
(it-> (+ 13 char-code)
(mod it 26)))
code-vec ))
(defn transcode
[arg-str]
(it->
(str->codes arg-str)
(codes->baseline it)
(rot13 it)
(baseline->codes it)
(codes->str it)))
(deftest t1
(let [x1 (spyx (str->codes "hello"))
x2 (spyx (codes->baseline x1))
x3 (spyx (rot13 x2))
]
(is (= [104 101 108 108 111]) x2)
(is (= [7 4 11 11 14]) x2)
(is (= [20 17 24 24 1] x3))
(is (= "uryyb" (transcode "hello")))
(is (= "hello" (transcode (transcode "hello"))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment