FastPrepEncrypt Text by Columnar Transposition

Encrypt Text by Columnar Transposition

Palantir logoPalantir● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

For this exercise, use the following columnar transposition rule. Write the characters of text from left to right into rows with columns positions each. The last row may be incomplete. Read the occupied cells column by column, from the leftmost column to the rightmost, and within each column from top to bottom. Return the resulting ciphertext.

For this exercise, assume no padding is added. Preserve every input character, including spaces, punctuation, digits and letter case. Skip unoccupied cells and return an empty string for empty text.

Function

encryptTransposition(text: String, columns: int) → String

Examples

Example 1

text = "WEAREDISCOVERED"columns = 4return = "WECREDOEAIVDRSE"

The columns contain WECR, EDOE, AIVD and RSE, read in that order.

Example 2

text = "abcde"columns = 3return = "adbec"

Rows are abc and de. Read ad, be and c without adding padding.

Constraints

  • For this exercise, assume 0 <= text.length <= 500 and 1 <= columns <= 500.
  • For this exercise, assume every input character is printable ASCII, from character code 32 through 126.

More Palantir problems

See Palantir hiring insights
public String encryptTransposition(String text, int columns) {
    // Write your code here.
    return "";
}
text"WEAREDISCOVERED"
columns4
expected"WECREDOEAIVDRSE"
Checking account…