Skip to content

Instantly share code, notes, and snippets.

@msszczep
Created December 8, 2014 00:22
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 msszczep/a436e25601a6a031f5e1 to your computer and use it in GitHub Desktop.
Save msszczep/a436e25601a6a031f5e1 to your computer and use it in GitHub Desktop.
Clojure Bridge exercise: Caesar Cipher
(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