Saturday, May 16, 2015

Exercism: "Crypto Square in Clojure"

I solved the Crypto Square problem in Clojure.

This is my solution:

(ns crypto-square
(:require [clojure.string :as clj-str]))
(defn- no-punctuation [c]
(or (Character/isLetter c)
(Character/isDigit c)))
(defn- remove-punctuation [text]
(clj-str/join "" (filter no-punctuation text)))
(defn normalize-plaintext [text]
(clj-str/lower-case (remove-punctuation text)))
(defn square-size [text]
(int (Math/ceil (Math/sqrt (count text)))))
(defn plaintext-segments [text]
(let [normalized-text (normalize-plaintext text)
segment-size (square-size normalized-text)]
(map clj-str/join (partition-all segment-size normalized-text))))
(defn- pad-segments [segments segments-size]
(map #(format (str "%-" segments-size "s") %) segments))
(defn- segments-in-columns [text]
(let [segments (plaintext-segments text)
segment-size (count (first segments))]
(apply map
#(clj-str/trim (apply str %&))
(pad-segments segments segment-size))))
(defn- remove-spaces [text]
(clj-str/replace text " " ""))
(defn normalize-ciphertext [text]
(->> text
segments-in-columns
(clj-str/join " ")))
(defn ciphertext [text]
(remove-spaces (normalize-ciphertext text)))
You can nitpick this solution here or see all the exercises I've done so far in this repository.

2 comments:

  1. Nice and clean solution.

    This was my solution for the same problem

    https://gist.github.com/BrunoBonacci/cb13fc05bd720652eb17#file-gistfile1-clj-L7

    The key part happen in line 7 where the rows are transposed into columns.

    bye
    Bruno

    ReplyDelete
    Replies
    1. Thank you Bruno!

      As soon as I have some time, I'll remove the intermediate tests from Exercism and refactor my solution using your approach.

      Best regards,
      Manuel

      Delete