forked from srgnk/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encryption.py
61 lines (48 loc) · 1.18 KB
/
encryption.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/python3
import sys
from math import sqrt
from math import ceil
def get_grid(number):
root = sqrt(number)
x = int(root//1)
y = ceil(root)
while x*y < number:
if x <= y:
x += 1
else:
y += 1
return (x, y)
def encryption(string):
string = string.strip().replace(' ', '')
str_len = len(string)
x, y = get_grid(str_len)
#print("x = {} y = {}".format(x, y))
grid = [ [ '' for i in range(x) ] for _j in range(y) ]
count = 0
x_ind = 0
y_ind = 0
for ind in range(str_len):
if count / y == 1 and count % y == 0:
count = 0
y_ind += 1
x_ind = 0
grid[x_ind][y_ind] = string[ind]
count += 1
x_ind += 1
#print(grid)
out = ''
for _i in range(y):
for _j in range(x):
out += grid[_i][_j]
out += ' '
#print(out)
#out = ''
#for _i in range(y):
# for _j in range(x):
# out += grid[_i][_j]
# out += ' '
return out
if __name__ == "__main__":
s = input().strip()
result = encryption(s)
print(result)