Exercism v3 launches on Sept 1st 2021. Learn more! ๐๐๐

Published at Jul 13 2018
·
2 comments

Instructions

Test suite

Solution

Implement the classic method for composing secret messages called a square code.

Given an English text, output the encoded version of that text.

First, the input is normalized: the spaces and punctuation are removed from the English text and the message is downcased.

Then, the normalized characters are broken into rows. These rows can be regarded as forming a rectangle when printed with intervening newlines.

For example, the sentence

```
"If man was meant to stay on the ground, god would have given us roots."
```

is normalized to:

```
"ifmanwasmeanttostayonthegroundgodwouldhavegivenusroots"
```

The plaintext should be organized in to a rectangle. The size of the
rectangle (`r x c`

) should be decided by the length of the message,
such that `c >= r`

and `c - r <= 1`

, where `c`

is the number of columns
and `r`

is the number of rows.

Our normalized text is 54 characters long, dictating a rectangle with
`c = 8`

and `r = 7`

:

```
"ifmanwas"
"meanttos"
"tayonthe"
"groundgo"
"dwouldha"
"vegivenu"
"sroots "
```

The coded message is obtained by reading down the columns going left to right.

The message above is coded as:

```
"imtgdvsfearwermayoogoanouuiontnnlvtwttddesaohghnsseoau"
```

Output the encoded text in chunks that fill perfect rectangles `(r X c)`

,
with `c`

chunks of `r`

length, separated by spaces. For phrases that are
`n`

characters short of the perfect rectangle, pad each of the last `n`

chunks with a single trailing space.

```
"imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau "
```

Notice that were we to stack these, we could visually decode the cyphertext back in to the original message:

```
"imtgdvs"
"fearwer"
"mayoogo"
"anouuio"
"ntnnlvt"
"wttddes"
"aohghn "
"sseoau "
```

J Dalbey's Programming Practice problems http://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

```
(ns crypto-square-test
(:require [clojure.test :refer [deftest is]]
crypto-square))
(deftest normalize-splunk
(is (= "splunk" (crypto-square/normalize-plaintext "s#!@$%plunk"))))
(deftest normalize-with-punctuation
(is (= "123go" (crypto-square/normalize-plaintext "1, 2, 3 GO!"))))
(deftest square-2
(is (= 2 (crypto-square/square-size "1234"))))
(deftest square-3
(is (= 3 (crypto-square/square-size "123456789"))))
(deftest square-4
(is (= 4 (crypto-square/square-size "123456789abc"))))
(deftest segments
(is (= ["neverv", "exthin", "eheart", "withid", "lewoes"]
(crypto-square/plaintext-segments "Never vex thine heart with idle woes."))))
(deftest segments-2
(is (= ["zomg", "zomb", "ies"]
(crypto-square/plaintext-segments "ZOMG! ZOMBIES!!!"))))
(deftest cipher-1
(is (= "tasneyinicdsmiohooelntuillibsuuml"
(crypto-square/ciphertext "Time is an illusion. Lunchtime doubly so."))))
(deftest cipher-2
(is (= "wneiaweoreneawssciliprerlneoidktcms"
(crypto-square/ciphertext "We all know interspecies romance is weird."))))
(deftest cipher-3
(is (= "msemo aanin dnin ndla etlt shui"
(crypto-square/normalize-ciphertext "Madness, and then illumination."))))
(deftest cipher-4
(is (= "vrel aepe mset paoo irpo"
(crypto-square/normalize-ciphertext "Vampires are people too!"))))
(deftest cipher-5
(is (= (str "ageihdsednsh lsagtoonaepe lannswnccair hrditeaetnrh "
"ueethdnatoio mbqyewdnotto aouayicdwhod nranatosaef "
"bnldrhnhrrb efirersodir irnieecusno nedgnailoat")
(let [plaintext (str "All human beings are born free "
"and equal in dignity and rights. "
"They are endowed with reason and conscience "
"and should act towards one another "
"in a spirit of brotherhood.")]
(crypto-square/normalize-ciphertext plaintext)))))
```

```
(require '[clojure.string :as str])
(defn- char-range
"Define a range of characters (inclusive)."
[start stop]
(map char (range (int start) (inc (int stop)))))
(def digits (char-range \0 \9))
(def alphabet (char-range \a \z))
(def valid-chararacters
(-> #{} (into digits) (into alphabet)))
;;; Utility functions
(defn- stringify
"Convert a sequence into a string."
[sequence]
(apply str sequence))
(defn- groups-of
"Group the sequence into chunks of size (last group may be short)."
[size s]
(if (empty? s) []
(loop [result [] s s]
(let [head (take size s)
tail (drop size s)
new-result (conj result head)]
(if (empty? tail) new-result
(recur new-result tail))))))
(defn- spaced-groups-of
"Introduce spaces at size intervals."
[size string]
(->> string
(groups-of size)
(map stringify)
(str/join " ")))
(defn- remove-spaces
"Remove spaces from a string."
[string]
(filter #(not= \space %) string))
(defn make-size
"Return a function that appends spaces to a group so that it is at size."
[size]
(fn [s]
(if (= size (count s)) s
(into (vec s) (take (- size (count s)) (cycle " "))))))
(defn- ensure-sized-groups
"Ensure all groups are at least size in length."
[size groups]
(map (make-size size) groups))
(defn- normalize-group-lengths
"Pad any short groups to the max length."
[groups]
(if (empty? groups) []
(let [size (apply max (map count groups))]
(ensure-sized-groups size groups))))
(defn- stringify-groups
"Join all groups into a single character string."
[groups]
(->> groups
(map stringify)
stringify))
(defn- remove-spaces-from-groups
"Remove any spaces from the grouped sequences."
[groups]
(map remove-spaces groups))
(defn- transpose-equal-sized-groups
"Same as tranaspose, but only works with equal sized groups."
[groups]
(apply map (cons str groups)))
(defn- transpose
"Transpose a group of sequences, resulting in another group of sequences."
[groups]
(->> groups
normalize-group-lengths
transpose-equal-sized-groups
remove-spaces-from-groups))
(defn size-bigger-than-square
"Return a predicate function that is true when size is bigger than
the square of its input."
[size]
(fn [n] (> size (* n n))))
;;; Public functions
(defn square-size
"Determine the size of the square needed to encode a normalized string."
[normalized-string]
(let [size (count normalized-string)]
(first (drop-while (size-bigger-than-square size)
(iterate inc 1)))))
(defn normalize-plaintext
"Normalize the plaintext by removing non-alphanumeric characters."
[string]
(->> string
(str/lower-case)
(filter valid-chararacters)
stringify))
(defn plaintext-segments
"Break a normalized string into appropriate sized segments."
[string]
(let [normal (normalize-plaintext string)
size (square-size normal)]
(->> normal
(groups-of size)
(map stringify))))
(defn ciphertext
"Generate a non-grouped string of encoded text."
[string]
(->> string
plaintext-segments
transpose
stringify-groups))
(defn normalize-ciphertext
[string]
(->> string
ciphertext
(spaced-groups-of 5)))
```

A huge amount can be learned from reading other peopleโs code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments

Renamed parameter to spaced-groups-of to make it clear it gets a string (or at least a sequence of characters).

Unless you wanted to implement some Clojure functions yourself, you might want to have a look at the following:

line 21: partition, partition-all line 32: partition, interpose line 40: clojure.string/trim line 97: Math/ceil, Math/sqrt line 109: clojure.string/replace #"\W" ""

Also, I'm not sure whether your usage of higher-order-functions (lines 48, 93) makes things more understandable.