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:

$\lfloor\sqrt{L}\rfloor\leq row\leq column\leq\lceil\sqrt{L}\rceil,\text{where}\lfloor x\rfloor\text{is floor function and}\lceil x\rceil\text{is cell function}$ 

Example  

$s=\textbf{if man was meant to stay on the ground good 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 $rows\times columns\geq L$   
If multiple grids satisfy the above conditions, choose the one with the minimum area, i.e. $rows\times 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 $\boldsymbol{\mathrm{~S~}}$

Constraints

$1\leq\ \text{length of}\ s\leq81$ 

$\boldsymbol{\mathrm{~S~}}$ contains characters in the range ascii[a-z] and space, ascii(32).

Sample Input
haveaniceday

Sample Output 0

hae and via ecy

Explanation 0  

$L=12$, $\sqrt{12}$ is between $3$ and $4$. 

Rewritten with $3$ rows and $4$ columns:

have
anic
eday

Sample Input 1

feedthedog    

Sample Output 1

fto ehg ee dd

Explanation 1  

$L=\textbf{10}$, $\sqrt{10}$ is between $3$ and $4$. 

Rewritten with $3$ rows and $4$ columns:  

feed
thed
og

Sample Input 2

chillout

Sample Output 2

clu hlt io

Explanation 2  

$L=8$, $\sqrt{8}$ is between $2$ and $3$. 

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

chi
llo
ut

Difficulty: MEDIUM

Solutions:
import itertools, sys
s = sys.stdin.readline().strip()
dim = int(len(s) ** 0.5)
if dim * dim == len(s):
	(x, y) = (dim, dim)
else:
	(x, y) = (dim, dim + 1)
array = [s[i:i + y] for i in range(0, len(s), y)]
transpose = itertools.zip_longest(*array, fillvalue='')
print(' '.join((''.join(s) for s in transpose)))


msg = input().strip()
from math import ceil, floor, sqrt

def enc(msg):
	sr = sqrt(len(msg))
	(row, col) = (floor(sr), ceil(sr))
	rt = []
	for i in range(col):
		tmp = []
		ind = i
		while ind < len(msg):
			tmp.append(msg[ind])
			ind += col
		tmp = ''.join(tmp)
		rt.append(tmp)
	return ' '.join(rt)
print(enc(msg))


import sys, math
message = sys.stdin.read().strip()
boxheight = math.ceil(math.sqrt(len(message)))
box = [[] for _ in range(boxheight)]
for (index, char) in enumerate(message):
	boxindex = index % boxheight
	box[boxindex].append(char)
print(' '.join((''.join((c for c in q)) for q in box)))


import sys
from itertools import islice
text = ''.join(sys.stdin.readline().strip())
n = len(text)
q = int(pow(n, 0.5))
(a, b) = (n, n)
for i in range(q, q + 2):
	for j in range(q, q + 2):
		if i <= j and i * j >= n and (i * j < a * b):
			(a, b) = (i, j)
ans = []
for i in range(b):
	ans.append(''.join(islice(text, i, None, b)))
print(' '.join(ans))


from math import sqrt, floor, ceil
from itertools import zip_longest

def main():
	line = input().strip()
	length = len(line)
	sroot = sqrt(length)
	(low, high) = (floor(sroot), ceil(sroot))
	l = []
	for i in range(0, length, high):
		l.append(line[i:i + high])
	print(' '.join(list(map(lambda x: ''.join(x), zip_longest(*l, fillvalue='')))))
main()


s = input()
n = 1
while n * n < len(s):
	n += 1
a = s + ' ' * (n * n - len(s))
a = [a[i:i + n] for i in range(0, n * n, n)]
print(' '.join([''.join([a[j][i] for j in range(n)]).strip() for i in range(n)]))


import math
in_text = input().strip()
len_sqrt = math.sqrt(len(in_text))
columns = int(math.ceil(len_sqrt))
rows = int(math.ceil(len_sqrt))
if int(len(in_text) / columns) < rows - 1:
	rows -= 1
encoded = ''
for c in range(columns):
	for r in range(rows):
		if r * columns + c < len(in_text):
			encoded += in_text[r * columns + c]
	if c < columns:
		encoded += ' '
print(encoded)


from math import floor
from math import sqrt
from math import ceil

def rect_height(l):
	root = sqrt(l)
	(_min, _max) = (int(floor(root)), int(ceil(root)))
	if _min ** 2 >= l:
		return _min
	else:
		return _max

def encode(s):
	l = len(s)
	height = rect_height(l)
	rect = []
	for i in range(height):
		word = []
		for j in range(i, l, height):
			word.append(s[j])
		rect.append(''.join(word))
	return ' '.join(rect)
s = input().strip()
print(encode(s))


import math
word = input().strip()
word.replace(' ', '')
height = math.floor(math.sqrt(len(word)))
width = math.ceil(math.sqrt(len(word)))
while height * width < len(word):
	height = height + 1
word = word + '                                '
for i in range(0, width):
	for j in range(0, height):
		print(word[i + width * j].replace(' ', ''), end='')
	print(' ', end='')
print('')


from math import *
P = [x for x in input().strip()]
L = len(P)
H = W = int(L ** 0.5)
if H * W < len(P):
	W += 1
if H * W < len(P):
	H += 1
P += [''] * (H * W - len(P))
s = ''
for w in range(0, W):
	for h in range(0, H):
		s += P[h * W + w]
	s += ' '
s = s[:-1]
print(s)


import math
intext = input()
n = len(intext)
minW = math.floor(math.sqrt(n))
maxW = math.ceil(math.sqrt(n))
maxi = 9999999
pair = (99999, 99999)
for i in range(minW, maxW + 1):
	for j in range(i, maxW + 1):
		if i * j >= n:
			if i * j < maxi:
				maxi = i * j
				pair = (i, j)
intext = intext.ljust(pair[0] * pair[1])
cube = []
for i in range(pair[0]):
	cube.append(intext[i * pair[1]:i * pair[1] + pair[1]])
coded = ''
for i in range(pair[1]):
	tmp = ''
	for j in range(pair[0]):
		tmp += cube[j][i]
	tmp = tmp.strip()
	coded += tmp + ' '
print(coded)


import sys
import math
for line in sys.stdin:
	string = line.strip()
	width = length = 1
	while width * length < len(string):
		width += 1
		if width * length < len(string):
			length += 1
	print(' '.join((string[i::width] for i in range(width))))


import math
x = input().strip()
l = len(x)
a = math.floor(math.sqrt(l))
b = math.ceil(math.sqrt(l))
if a != math.sqrt(l):
	a += 1
for i in range(a):
	j = 0
	while i + j < l:
		print(x[i + j], end='')
		j += b
	print(' ', end='')


import sys
from math import ceil, floor, sqrt

def decode(string):
	l = sqrt(len(string))
	possibles = [(floor(l), floor(l)), (floor(l), ceil(l)), (ceil(l), ceil(l))]
	(rows, cols) = min([(row, ceil) for (row, ceil) in possibles if row * ceil >= len(string)])
	answer = [string[i::cols] for i in range(cols)]
	return ' '.join(answer)

def main():
	print(decode(sys.stdin.readline().strip()))
main()


import sys
from math import floor, ceil, sqrt
word = sys.stdin.readline().strip()
width = height = floor(sqrt(len(word)))
maxheight = ceil(sqrt(len(word)))
while width * height < len(word) and width <= height and (height <= maxheight):
	if width == height:
		height += 1
	elif (width + 1) * height > width * (height + 1):
		width += 1
	else:
		height += 1
encrypted = ''
enc = []
if width != height:
	width += 1
for j in range(width):
	for i in range(height):
		if height * i + j < len(word):
			encrypted += word[height * i + j]
	enc.append(encrypted)
	encrypted = ''
print(' '.join(enc))


from math import ceil, sqrt
plaintext = str(input()).strip()
width = ceil(sqrt(len(plaintext)))
ciphertext = ''
for i in range(width):
	if len(ciphertext) > 0:
		ciphertext += ' '
	for j in range(i, len(plaintext), width):
		ciphertext += plaintext[j]
print(ciphertext)


import sys, collections, math, itertools
n = input()

def encryption(n):
	n = n.replace(' ', '')
	height = math.ceil(len(n) ** (1 / 2))
	width = math.floor(len(n) ** (1 / 2))
	minarea = sys.maxsize
	(h, w) = (0, 0)
	for i in range(width, height + 1):
		for j in range(1, height + 1):
			if i * j >= len(n) and i * j < minarea:
				minarea = i * j
				(w, h) = (i, j)
	p = list(n)
	matrix = [p[i:i + h] for i in range(0, len(p), h)]
	res = ''
	almost = list(itertools.zip_longest(*matrix, fillvalue=''))
	for i in almost:
		res += ''.join(i)
		res += ' '
	return res[:-1]
print(encryption(n))


import sys
import math
text = sys.stdin.readline().strip()
sqrt = math.sqrt(len(text))
num_rows = math.floor(sqrt)
num_columns = math.ceil(sqrt)
for i in range(num_columns):
	for j in range(i, len(text), num_columns):
		print(text[j], end='')
	print(' ', end='')


from math import sqrt, ceil, floor
s = input().strip()
res = ''
rows = floor(sqrt(len(s)))
cols = ceil(sqrt(len(s)))
if rows * cols < len(s):
	rows += 1
for col in range(cols):
	for row in range(rows):
		idx = col + row * cols
		if idx < len(s):
			res += s[idx]
	res += ' '
print(res)


from math import sqrt, ceil
word = str(input().strip())
n = len(word)
width = ceil(sqrt(n))
height = ceil(n / width)
s = ''
for j in range(width):
	for i in range(height):
		if i * width + j < len(word):
			s += word[i * width + j]
	s += ' '
print(s)


import sys
import math

def opt_size(x):
	rows_min = math.floor(math.sqrt(x))
	collumns_min = math.floor(math.sqrt(x))
	rows_max = math.ceil(math.sqrt(x))
	collumns_max = math.ceil(math.sqrt(x))
	smallest = [rows_max * collumns_max, rows_max, collumns_max]
	for rows in range(rows_min, rows_max + 1):
		for collumns in range(rows_min, rows_max + 1):
			if rows * collumns < smallest[0] and rows * collumns >= x:
				smallest = [rows * collumns, rows, collumns]
	(rows, collumns) = smallest[1:]
	return (rows, collumns)

def make_rectangle(s):
	(height, width) = opt_size(len(s))
	rect = []
	for a in range(height):
		row = []
		for b in range(width):
			row.append('')
		rect.append(row)
	for a in range(height):
		for b in range(width):
			if a * width + b < len(s):
				rect[a][b] = s[a * width + b]
			else:
				rect[a][b] = ''
	return rect

def transponse(matrix):
	y = len(matrix)
	x = len(matrix[0])
	tr = []
	for a in range(x):
		row = []
		for b in range(y):
			row.append('')
		tr.append(row)
	for a in range(x):
		for b in range(y):
			tr[a][b] = matrix[b][a]
	return tr

def tests():
	t = opt_size
	assert t(9) == (3, 3)

def main():
	text = sys.stdin.readline().strip()
	text_rect = make_rectangle(text)
	text_rect = transponse(text_rect)
	print(' '.join([''.join([str(i) for i in row]) for row in text_rect]))
main()


import itertools, sys
s = sys.stdin.readline().strip()
dim = int(len(s) ** 0.5)
if dim * dim == len(s):
	(x, y) = (dim, dim)
else:
	(x, y) = (dim, dim + 1)
array = [s[i:i + y] for i in range(0, len(s), y)]
transpose = itertools.zip_longest(*array, fillvalue='')
print(' '.join((''.join(s) for s in transpose)))


import re
import math
s = input()
c = math.ceil(math.sqrt(len(s)))
l = re.findall('\\S{0,%d}' % c, s)
d = ''
for x in range(c):
	for y in l:
		try:
			d += y[x]
		except:
			d += ''
	d += ' '
print(d)


from math import sqrt, ceil, floor
word = input().strip()
width = floor(sqrt(len(word)))
height = ceil(sqrt(len(word)))
smallest = len(word) * 2
for h in range(1, height + 1):
	for w in range(h, ceil(len(word) / h) + 1):
		if len(word) <= w * h and w - h < smallest:
			width = w
			height = h
			smallest = w - h
word += ' ' * (width * height - len(word))
encrypted = []
for i in range(width):
	enc_word = ''
	for j in range(height):
		enc_word += word[j * width + i]
	encrypted.append(enc_word.strip())
print(' '.join(encrypted))


import math
message = input().replace(' ', '')
height = math.ceil(math.sqrt(len(message)))
width = math.ceil(math.sqrt(len(message)))
while height * width < len(message):
	width += 1
res = ''
for c in range(width):
	if len(res) > 0 and res[-1] != ' ':
		res += ' '
	for r in range(height):
		if width * r + c < len(message):
			res += message[width * r + c]
print(res)


from math import sqrt, floor, ceil
word = input()
n = len(word)
min_dist = floor(sqrt(n))
if min_dist ** 2 == n:
	width = min_dist
else:
	width = min_dist + 1
height = ceil(n / width)
rect = width * height
padded = word + ' ' * (rect - n)
chunks = [padded[i * width:i * width + width] for i in range(height)]
transpose = zip(*chunks)
unpadded = [''.join([c for c in chunk if not c == ' ']) for chunk in transpose]
coded = ' '.join(unpadded)
print(coded)


import math
Input = [i for i in input().strip()]
l = len(Input)
width = math.floor(math.sqrt(l))
height = math.ceil(math.sqrt(l))
if width * height < l:
	width = height
ans = ''
for i in range(height):
	for j in range(width):
		if i + height * j < l:
			ans += Input[i + height * j]
	ans += ' '
print(ans)


import math
a = input()
n = len(a)
h = math.floor(math.sqrt(n))
w = h
if w * h < n:
	w += 1
if w * h < n:
	h += 1
a += ' ' * (w * h - n)
chunks = [a[i:i + w] for i in range(0, n, w)]
transp = zip(*chunks)
print(' '.join((''.join(s).strip() for s in transp)))


from math import sqrt
from math import ceil
message = input().replace(' ', '')
size = ceil(sqrt(len(message)))
tab = []
for i in range(size):
	tab.append([])
	for j in range(size):
		if size * i + j >= len(message):
			tab[i].append('')
		else:
			tab[i].append(message[size * i + j])
for i in range(size):
	for j in range(size):
		print(tab[j][i], end='')
	print('', end=' ')


import math
string = input().strip()
flooring = math.floor(math.sqrt(len(string)))
ceiling = math.ceil(math.sqrt(len(string)))
width = flooring if flooring > ceiling else ceiling
encrypt = []
for i in range(len(string)):
	if i % width == 0:
		encrypt.append([string[i]])
	else:
		encrypt[-1].append(string[i])
for i in encrypt:
	if len(i) != width:
		while len(i) < width:
			i.append('')
for i in range(0, width):
	for j in encrypt:
		if j != '':
			print(j[i], end='')
	print(' ', end='')


import math

def encrypt(word, num_rows, num_columns):
	output = []
	for curr in range(num_rows + 1):
		while curr < len(word):
			output.append(word[curr])
			curr += num_columns
		output.append(' ')
	return ''.join(output)
word = input().strip()
num_rows = math.floor(math.sqrt(len(word)))
num_columns = math.ceil(math.sqrt(len(word)))
if num_rows == num_columns:
	num_rows -= 1
print(encrypt(word, num_rows, num_columns))


import itertools, sys
s = sys.stdin.readline().strip()
dim = int(len(s) ** 0.5)
if dim * dim == len(s):
	(x, y) = (dim, dim)
else:
	(x, y) = (dim, dim + 1)
array = [s[i:i + y] for i in range(0, len(s), y)]
transpose = itertools.zip_longest(*array, fillvalue='')
print(' '.join((''.join(s) for s in transpose)))


import itertools, sys
s = sys.stdin.readline().strip()
dim = int(len(s) ** 0.5)
if dim * dim == len(s):
	(x, y) = (dim, dim)
else:
	(x, y) = (dim, dim + 1)
array = [s[i:i + y] for i in range(0, len(s), y)]
transpose = itertools.zip_longest(*array, fillvalue='')
print(' '.join((''.join(s) for s in transpose)))


import math

def encrypt(s):
	s = s.replace(' ', '')
	sqrt = math.sqrt(len(s))
	rowcount = math.floor(sqrt)
	colcount = math.ceil(sqrt)
	if rowcount * colcount < len(s):
		rowcount += 1
	square = [s[i:i + colcount] for i in range(0, len(s), colcount)]
	s += ' ' * colcount
	transposed = [''.join((s[r * colcount + col] for r in range(rowcount))) for col in range(colcount)]
	return ' '.join((t.strip() for t in transposed))
print(encrypt(input()))


import math
lst = str(input())
num = math.sqrt(len(lst))
length = math.ceil(num)
width = math.floor(num)
rem = length - len(lst) % length
lst += str(rem * ' ')
lst = list(lst)
string = ''
nlst = [lst[i:i + length] for i in range(0, len(lst), length)]
for i in range(length):
	for j in range(len(nlst)):
		if nlst[j][i] != ' ':
			string += nlst[j][i]
	string += ' '
print(string)


from math import sqrt, floor, ceil
word = input().strip()
n = len(word)
nSqrt = sqrt(n)
minWidth = floor(nSqrt)
maxHeight = ceil(nSqrt)
width = minWidth
height = maxHeight
while width * height < n or width < height:
	width += 1
while width * height >= n:
	height -= 1
height += 1
encodedMsg = ''
for i in range(width):
	for j in range(height):
		index = j * width + i
		if index >= n:
			break
		encodedMsg += word[index]
	if i != width - 1:
		encodedMsg += ' '
print(encodedMsg)