🐍Python/Python_알고리즘

[알고리즘] 50. Encryption

728x90
반응형

An English text needs to be encrypted using the following encryption scheme.
First, the spaces are removed from the text. Let  be the length of this text.
Then, characters are written into a grid, whose rows and columns have the following constraints:

For example, the sentence , after removing spaces is  characters long.  is between  and , 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 
  • If multiple grids satisfy the above conditions, choose the one with the minimum area, i.e. .

The encoded message is obtained by displaying the characters in a column, inserting a space, and then displaying the next column and inserting a space, and so on. For example, the encoded message for the above rectangle is:

imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau

You will be given a message to encode and print.

Function Description

Complete the encryption function in the editor below. It should return a single string composed as described.

encryption has the following parameter(s):

  • s: a string to encrypt

Input Format

One line of text, the string 

Constraints


 is comprised only of characters in the range ascii[a-z].

Output Format

Print the encoded message on one line as described.

Sample Input

haveaniceday

Sample Output 0

hae and via ecy

Explanation 0

 is between  and .
Rewritten with  rows and  columns:

have
anic
eday

Sample Input 1

feedthedog    

Sample Output 1

fto ehg ee dd

Explanation 1

 is between  and .
Rewritten with  rows and  columns:

feed
thed
og

Sample Input 2

chillout

Sample Output 2

clu hlt io

Explanation 2

 is between  and .
Rewritten with  columns and  rows ( so we have to use .)

chi
llo
ut

답 :



#!/bin/python3
from math import floor,sqrt,ceil
import math
import os
import random
import re
import sys

# Complete the encryption function below.
if __name__ == '__main__':
s = input()
r = floor(sqrt(len(s)))
c = ceil(sqrt(len(s)))
wordList = list()
res = ''
for i in range(floor(len(s)/r)):
wordList.append(s[i*c:c*(i+1)])
for i in range(c):
res += s[i::c] + ' '
print(res)

주어진 문자열의 길이를 제곱근 한 수를 사이에 두고 row,col을 정한다. 


그리고 그 범위 내에서 columns 수 만큼 문자열을 슬라이싱하고, 행렬상으로 봤을 때 열을 따라 즉 [0]끼리 [1]끼리 묶어서 프린트한다!

728x90
반응형