-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryption.py
More file actions
executable file
·40 lines (30 loc) · 870 Bytes
/
encryption.py
File metadata and controls
executable file
·40 lines (30 loc) · 870 Bytes
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
#!/usr/bin/env python3
import math
import os
import sys
from pathlib import Path
from typing import IO
def encryption(s: str) -> str:
s_len = len(s)
rows = math.floor(math.sqrt(s_len))
columns = math.ceil(math.sqrt(s_len))
while rows * columns < s_len:
if rows < columns:
rows += 1
else:
columns += 1
matrix = [s[i : i + columns] for i in range(0, s_len, columns)]
return " ".join(
"".join(line[i] for line in matrix if i < len(line)) for i in range(columns)
)
def main(fptr: IO) -> None:
s = input().rstrip()
result = encryption(s)
fptr.write(result + "\n")
if __name__ == "__main__":
if path := os.getenv("OUTPUT_PATH"):
with Path(path).open("wt", encoding="utf-8") as fptr:
main(fptr)
fptr.close()
else:
main(sys.stdout)