Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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/

@vpetruchok
Copy link

vpetruchok commented Jun 30, 2021

(def en-alphabet "abcdefghijklmnopqrstuvwxyz")

(defn atbash-mk-translation-table [alphabet]
  (->> alphabet
       (map-indexed (fn [i el]
                      [el (nth alphabet (- (dec (count alphabet)) i))]))
       (into {})))

(def translation-table
  (merge (atbash-mk-translation-table en-alphabet)
         (atbash-mk-translation-table (clojure.string/upper-case en-alphabet))))

(defn encode [ch]
  (get translation-table ch ch))

(defn atbash [s]
  (if (empty? s)
    ""
    (->> s
         (map encode)
         (clojure.string/join))))

@KubqoA
Copy link

KubqoA commented Jul 1, 2021

(defn char-range [from to] (map char (range (int from) (+ (int to) 1))))
(defn mirror-part [xs] (zipmap xs (reverse xs)))
(def mirror (merge (mirror-part (char-range \A \Z)) (mirror-part (char-range \a \z))))

(defn atbash [s] (apply str (replace mirror s)))

@alex-gerdom
Copy link

alex-gerdom commented Jul 1, 2021

(defn atbash [s]
   (let [A-Z (map char (range 65  91))
         a-z (map char (range 97 123))
         conversions (merge (zipmap A-Z (reverse A-Z))
                            (zipmap a-z (reverse a-z)))]
     (->> s
          (map #(get conversions % %))
          (clojure.string/join ""))))
  • Edited for off by one error for ascii code point of Z.

@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