-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathencryption.js
More file actions
56 lines (51 loc) · 1.25 KB
/
encryption.js
File metadata and controls
56 lines (51 loc) · 1.25 KB
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
function encryption(s) {
var strArr=[];
var len=0;
//Remove spaces
for(var i=0; i<s.length; i++){
if(s.charAt(i)!=' '){
strArr.push(s.charAt(i));
len++;
}
}
var row= Math.floor(Math.sqrt(len));
var column= Math.ceil(Math.sqrt(len));
while((row*column)<len){
//Column has the max value so increament the row
row++;
}
var arrMatrix= new Array(row);
for(var i=0; i<row; i++){
arrMatrix[i]= new Array(column);
}
var count=0;
for(var i=0; i<row; i++){
for(var j=0; j<column; j++){
if(count==len){
break;
}
else{
arrMatrix[i][j]= strArr[count];
count++;
}
}
}
var finalArr=[];
for(var i=0; i<column; i++){
for(var j=0; j<row; j++){
if(arrMatrix[j][i]!=undefined){
finalArr.push(arrMatrix[j][i]);
}
}
finalArr.push(" ");
}
var finalStr=finalArr.join("");
return finalStr;
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const s = readLine();
let result = encryption(s);
ws.write(result + "\n");
ws.end();
}