Encryption

Encryption

An English text needs to be encrypted using the following encryption scheme.

First, the spaces are removed from the text. Let L be the length of this text.

Then, characters are written into a grid, whose rows and columns have the following constraints:

For example, the sentence s = if man was meant to stay on the ground god would have given us roots, after removing spaces is 54 characters long. is between 7 and 8, so it is written in the form of a grid with 7 rows and 8 columns.

1
2
3
4
5
6
7
ifmanwas
meanttos
tayonthe
groundgo
dwouldha
vegivenu
sroots
  • Ensure that
  • If multiple grids satisfy the above conditions, choose the one with the minimum area, i.e. .

The encoded message is obtained by displaying the characters in a column, inserting a space, and then displaying the next column and inserting a space, and so on. For example, the encoded message for the above rectangle is:

1
imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau

You will be given a message to encode and print.

Function Description

Complete the encryption function in the editor below. It should return a single string composed as described.

encryption has the following parameter(s):

  • s: a string to encrypt

Input Format

One line of text, the string S

Constraints

s is comprised only of characters in the range ascii[a-z].

Output Format

Print the encoded message on one line as described.

Sample Input

1
haveaniceday

Sample Output 0

1
hae and via ecy

Explanation 0

L = 12, is between 3 and 4.

Rewritten with 3 rows and 4 columns:

1
2
3
have
anic
eday

Sample Input 1

1
feedthedog

Sample Output 1

1
fto ehg ee dd

Explanation 1

L = 10, is between 3 and 4.

Rewritten with 3 rows and 4 columns:

1
2
3
feed
thed
og

Sample Input 2

1
chillout

Sample Output 2

1
clu hlt io

Explanation 2

L = 8, is between 2 and 3.

Rewritten with 3 columns and 3 rows (2 * 3 = 6 < 8 so we have to use 3 x 3.)

1
2
3
chi
llo
ut

Solution

1
2
3
4
5
6
7
8
9
10
11
12
// Complete the encryption function below.
function encryption(s) {
let value = Math.ceil(Math.sqrt([...s].length));

return [...s]
.reduce((target, item, index) => {
target[index % value] += item;

return target;
}, new Array(value).fill(""))
.join(" ");
}
Buy Me A Coffee

Comments

10Days of JS 30Days of Code Algorithm Android Debug Bridge Android Debugging Basic for Web Blog Browsers Chrome으로 Android Debugging 방법 Correctness and the Loop Invariant hackerrank solution in javascript Debug Tools Development Environment in MacOS ES6 Front-End Funny String of Algorithms hackerrank solution in javascript Funny String of Algorithms hackerrank solution in typescript Generator Github Page with Hexo Github Pages HackerRank HackerRank in a String of Algorithms hackerrank solution in javascript HackerRank in a String of Algorithms hackerrank solution in typescript Hexo Hexo Icarus theme Hexo 블로그 만들기 Hexo+Github How Browsers work Insertion Sort - Part 1 hackerrank solution in javascript Insertion Sort - Part 2 hackerrank solution in javascript JS Library JavaScript Level1 Level2 Loops MacOS 개발 환경 설정하기 Mobile web Debugging Node.js Pangrams of Algorithms hackerrank solution in javascript Pangrams of Algorithms hackerrank solution in typescript Problem Solving Programmers Quicksort 1 - Partition hackerrank solution in javascript React RoadMap Running Time of Algorithms hackerrank solution in javascript Safari Debugging Safari Technology Preview Settings Sorting String Strings Strong Password of Algorithms hackerrank solution in javascript TypeScript blog iPhone Safari Debugging 방법 insertion sort 모바일 웹 디버깅 아이폰 사파리를 디버깅하는 방법 안드로이드 디버그 브리지 안드로이드 디버깅하는 방법
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×