forked from adityabisoi/ds-algo-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.py
68 lines (44 loc) · 1.3 KB
/
solution.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
62
63
64
65
66
67
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the encryption function below.
def encryption(s):
#length L of string
L = len(s)
#final outup string
fin_str=""
#initializing rows and columns
row = 0
col = 0
#removing all spaces from the string
s = s.replace(" ", "")
#number of rows
row = math.floor(math.sqrt(L))
#number of columns
col = math.ceil(math.sqrt(L))
#ensuring rows x columns >=L
if(row * col < L):
row += 1
encrypted_grid = [[None for _ in range(col)] for _ in range(row)]
#Forming the grid
for i in range(row):
j=0
for j in range(col):
if((col*i+j)<L):
encrypted_grid[i][j] = s[(col * i)+j]
#forming fin_str by cllecting all the characters along a column first
for i in range(col):
for j in range(row):
if(encrypted_grid[j][i]!= None):
fin_str+=encrypted_grid[j][i]
fin_str+=" " #adding a space after each encrypted word
return fin_str
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
s = input()
result = encryption(s)
fptr.write(result + '\n')
fptr.close()