Skip to content
the poor coder

Hackerrank - Encryption Solution

Poor Man
Published
Updated
Reading time
3 min read

An English text needs to be encrypted using the following encryption scheme.
First, the spaces are removed from the text. Let  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 , after removing spaces is  characters long.  is between  and , so it is written in the form of a grid with 7 rows and 8 columns.

Text
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:

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

Constraints


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

Output Format

Print the encoded message on one line as described.

Sample Input

Text
haveaniceday

Sample Output 0

Text
hae and via ecy

Explanation 0

,  is between  and .
Rewritten with  rows and  columns:

Text
have
anic
eday

Sample Input 1

Text
feedthedog    

Sample Output 1

Text
fto ehg ee dd

Explanation 1

,  is between  and .
Rewritten with  rows and  columns:

Text
feed
thed
og

Sample Input 2

Text
chillout

Sample Output 2

Text
clu hlt io

Explanation 2

,  is between  and .
Rewritten with  columns and  rows ( so we have to use .)

Text
chi
llo
ut

Solution in Python#

Python
import math
from collections import defaultdict

def encryption(text):
    text = text.replace(" ","")
    root= math.sqrt(len(text))
    r = math.floor(root)
    c = math.ceil(root)
    d = defaultdict(str)
    for i in range(0,len(text),c):
        sub = text[i:i+c]
        for x in range(len(sub)):
            d[x]+=sub[x]
    return(list(d.values()))

print(*encryption(input()))

Explanation#

We remove any empty spaces from our text

Text
text = text.replace(" ","")

Then we use math.floor and math.ceil function to calculate the required number of rows and columns denoted by r and c respectively.

Text
root= math.sqrt(len(text))
r = math.floor(root)
c = math.ceil(root)

Using default dict we will initialize an empty dictionary.

Text
d = defaultdict(str)

We use for loop to divide our string into equal number of columns

Text
for i in range(0,len(text),c):
    sub = text[i:i+c]
    print(sub)

Example

Text
>>> haveaniceday
have
anic
eday

Instead of printing the value of our variable sub we add each letter to our default dict

Text
Loop 1 >>> d = {0:"h", 1:"a", 2:"v", 3:"e"}
Loop 2 >>> d = {0:"ha", 1:"an", 2:"vi", 3:"ec"}
Loop 3 >>> d = {0:"hae", 1:"and", 2:"via", 3:"ecy"}
Text
>>> list(d.values())
['hae', 'and', 'via', 'ecy']

Now the final step remaining is to print the values separated by a space

Text
>>> d = list(d.values())
>>> print(*d)
hae and via ecy
>>> print(" ".join(d))
hae and via ecy

Keep going

ESC