Skip to content

Instantly share code, notes, and snippets.

@ericnormand
Created June 28, 2021 16:32
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 ericnormand/4bca050e029545069d0e84b827fc8123 to your computer and use it in GitHub Desktop.
Save ericnormand/4bca050e029545069d0e84b827fc8123 to your computer and use it in GitHub Desktop.
432 PurelyFunctional.tv Newsletter

Atbash Cipher

The Atbash Cipher is simple: replace every letter with its "mirror" in the alphabet. A is replaced by Z. B is replaced by Y. Etc. Write a function to calculate it.

Examples

(atbash "") ;=> ""
(atbash "hello") ;=> "svool"
(atbash "Clojure") ;=> "Xolqfiv"
(atbash "Yo!") ;=> "Bl!"

Please maintain capitalization and non-alphabetic characters.

Thanks to this site for the problem idea, where it is rated Very Hard in Python. The problem has been modified.

Please submit your solutions as comments on this gist.

To subscribe: https://purelyfunctional.tv/newsletter/

@KingCode
Copy link

KingCode commented Oct 7, 2021

(def mirror (let [lks "abcdefghijklmnopqrstuvwxyz", 
                  uks (clojure.string/upper-case lks)
                  lvs (reverse lks), 
                  uvs (reverse uks)]
              (-> (zipmap lks lvs)
                  (merge (zipmap uks uvs)))))

(defn atbash [msg]
  (->> msg
       (map #(get mirror % %))
       (apply str)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment