Encryption

Encryption - A HackerRank challenge solution

Problem

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:

Example

s = if man was meant to stay on the ground god would have given us roots

After removing spaces, the string is 54 characters long. sqrt(54) is between 7 and 8, 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 row X columns >= L
  • If multiple grids satisfy the above conditions, choose the one with the minimum area, i.e. rows X columns.

The encoded message is obtained by displaying the characters of each column, with a space between column texts. The encoded message for the grid above is:

imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau

Create a function to encode a message.

Function Description

Complete the encryption function in the editor below.

encryption has the following parameter(s):

  • string s: a string to encrypt

Returns

  • string: the encrypted string

Input Format

  • One line of text, the string s

Constraints

1 <= length of s <= 81

s contains characters in the range ascii[a-z] and space, ascii(32).

My Solution

I’m providing the solution for Python and JS, please leave on the comments if you found a better way.

The actual solution is pretty simple if you got to understand the problem. The first time I read it wrong, and I missed some important data, so make sure you read carefuly through the problem and the examples they provide.

Si te gusta el contenido, por favor apoya mi trabajo!

Juan Cruz Martinez - Author @ Live Code Stream

Juan Cruz Martinez

¡Hola!

Soy Juan Cruz Martinez, el fundador de LiveCodeStream.dev. Me encanta programar y creo en el poder de la programación no solo para construir un mundo mejor, sino para hacer que TU vida sea mejor.

Fundé LiveCodeStream.dev porque quería ayudarte a aprender a programar, construir una mejor carrera y, en última instancia, crear una vida mejor. Eso me sucedió hace más de una década cuando comencé a programar, y le sucede a decenas de miles de personas a diario. Quiero que TÚ te unas a esa revolución.

No importa cómo lo llames, escribir código, programación, desarrollo de software o cualquier otra cosa, las habilidades involucradas están en una demanda cada vez mayor. Y ya sea que quieras incursionar en el desarrollo de sitios web, diseño de juegos, desarrollo de blockchain o cualquier otra cosa, quiero ayudarte.