 # rootulp's solution

## to Crypto Square in the Java Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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 "
``````

# Running the tests

You can run all the tests for an exercise by entering

``````\$ gradle test
``````

## Source

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

## Submitting Incomplete Solutions

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

### CryptoSquareTest.java

``````import org.junit.Test;
import org.junit.Ignore;

import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class CryptoSquareTest {

@Test
public void emptyPlaintextResultsInEmptyCiphertext() {
CryptoSquare cryptoSquare = new CryptoSquare("");
String expectedOutput = "";

assertEquals(expectedOutput, cryptoSquare.getCiphertext());
}

@Ignore("Remove to run test")
@Test
public void lettersAreLowerCasedDuringEncryption() {
CryptoSquare cryptoSquare = new CryptoSquare("A");
String expectedOutput = "a";

assertEquals(expectedOutput, cryptoSquare.getCiphertext());
}

@Ignore("Remove to run test")
@Test
public void spacesAreRemovedDuringEncryption() {
CryptoSquare cryptoSquare = new CryptoSquare(" b ");
String expectedOutput = "b";

assertEquals(expectedOutput, cryptoSquare.getCiphertext());
}

@Ignore("Remove to run test")
@Test
public void punctuationIsRemovedDuringEncryption() {
CryptoSquare cryptoSquare = new CryptoSquare("@1,%!");
String expectedOutput = "1";

assertEquals(expectedOutput, cryptoSquare.getCiphertext());
}

@Ignore("Remove to run test")
@Test
public void nineCharacterPlaintextResultsInThreeChunksOfThreeCharacters() {
CryptoSquare cryptoSquare = new CryptoSquare("This is fun!");
String expectedOutput = "tsf hiu isn";

assertEquals(expectedOutput, cryptoSquare.getCiphertext());
}

@Ignore("Remove to run test")
@Test
public void eightCharacterPlaintextResultsInThreeChunksWithATrailingSpace() {
CryptoSquare cryptoSquare = new CryptoSquare("Chill out.");
String expectedOutput = "clu hlt io ";

assertEquals(expectedOutput, cryptoSquare.getCiphertext());
}

@Ignore("Remove to run test")
@Test
public void fiftyFourCharacterPlaintextResultsInSevenChunksWithTrailingSpaces() {
CryptoSquare cryptoSquare = new CryptoSquare("If man was meant to stay on the ground, god would have given us roots.");
String expectedOutput = "imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn  sseoau ";

assertEquals(expectedOutput, cryptoSquare.getCiphertext());
}
}``````
``````import java.util.List;
import java.util.Arrays;
import java.util.ArrayList;

public class Crypto {

private String normalizedPlaintext;
private int squareSize;
private int height;
private char[][] matrix;
private char[][] transposedMatrix;

public Crypto(String input) {
this.normalizedPlaintext = setNormalizedPlaintext(input);
this.squareSize = setSquareSize(normalizedPlaintext);
this.height = setHeight(normalizedPlaintext);

this.matrix = buildMatrix();
this.transposedMatrix = transposeMatrix();
}

public String getNormalizedPlaintext() {
return normalizedPlaintext;
}

public int getSquareSize() {
return squareSize;
}

public List<String> getPlaintextSegments() {
return getSegments(matrix);
}

public String getCipherText() {
StringBuilder sb = new StringBuilder();

for (int i = 0; i < squareSize; i++) {
for (int j = 0; j < squareSize; j++) {
if (Character.isLetterOrDigit(transposedMatrix[i][j])) {
sb.append(transposedMatrix[i][j]);
}
}
}

return sb.toString();
}

public String getNormalizedCipherText() {
String cipherText = getCipherText();
StringBuilder sb = new StringBuilder();

for (int i = 0, n = cipherText.length(); i < n; i++) {
if (i != 0 && i % height == 0) { sb.append(" "); }
sb.append(cipherText.charAt(i));
}

return sb.toString();
}

private String setNormalizedPlaintext(String input) {
return input.toLowerCase().replaceAll("[^\\w\\d]", "");
}

private int setSquareSize(String normaliezedPlaintext) {
return (int) Math.ceil(Math.sqrt(normaliezedPlaintext.length()));
}

private int setHeight(String normaliezedPlaintext) {
return (int) Math.floor(Math.sqrt(normaliezedPlaintext.length()));
}

private char[][] buildMatrix() {
char[][] m = new char[squareSize][squareSize];

for (int i = 0; i < normalizedPlaintext.length(); i++) {
m[(int) Math.floor(i / squareSize)][i % squareSize] =
normalizedPlaintext.charAt(i);
}

return m;
}

private char[][] transposeMatrix() {
char[][] tp = buildMatrix();

for (int i = 0; i < squareSize; i++) {
for (int j = i + 1; j < squareSize; j++) {
char temp = tp[i][j];
tp[i][j] = tp[j][i];
tp[j][i] = temp;
}
}

return tp;
}

private List<String> getSegments(char[][] m) {
List<String> segments = new ArrayList<String>();

for (int i = 0; i < m.length; i++) {
String segment = new String(m[i]).trim();
}

return segments;
}

}``````