Hackerrank - Encryption Solution

Hackerrank - Encryption Solution

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.

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

haveaniceday

Sample Output 0

hae and via ecy

Explanation 0

,  is between  and .
Rewritten with  rows and  columns:

have
anic
eday

Sample Input 1

feedthedog    

Sample Output 1

fto ehg ee dd

Explanation 1

,  is between  and .
Rewritten with  rows and  columns:

feed
thed
og

Sample Input 2

chillout

Sample Output 2

clu hlt io

Explanation 2

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

chi
llo
ut

Solution in 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.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.

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

Using default dict we will initialize an empty dictionary.

d = defaultdict(str)

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

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

Example

>>> haveaniceday
have
anic
eday

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

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"}
>>> list(d.values())
['hae', 'and', 'via', 'ecy']

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

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

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe