Created
December 8, 2014 00:22
-
-
Save msszczep/a436e25601a6a031f5e1 to your computer and use it in GitHub Desktop.
Clojure Bridge exercise: Caesar 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
(def mystring "my name is stephanie") | |
(def alphabet "abcdefghijklmnopqrstuvwxyz") | |
(def alphabet-chars (map char alphabet)) | |
alphabet-chars | |
(def alphabet-shifted (drop 5 (take 100 (cycle alphabet-chars)))) | |
alphabet-shifted | |
(def shifted-map (zipmap alphabet-chars alphabet-shifted)) | |
(def shifted-map2 (assoc shifted-map \space \space)) | |
shifted-map2 | |
(map char mystring) | |
(map shifted-map2 (map char mystring)) | |
(apply str (map shifted-map2 (map char mystring))) | |
; == | |
(defn caesar-cipher [words offset] | |
"Assumes offset >=0, words entirely lowercase English characters or spaces" | |
(let [alphabet-chars (map char "abcdefghijklmnopqrstuvwxyz") | |
alphabet-shifted (->> (cycle alphabet-chars) (take 100) (drop offset)) | |
shifted-map (-> (zipmap alphabet-chars alphabet-shifted) | |
(assoc \space \space))] | |
(apply str (map shifted-map (map char words))))) | |
(caesar-cipher "hello my name is stephanie" 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment