특정 알고리즘의 사용보다는 주어진 조건을 차근차근 구현하는 문제.
def devide(message):
two_words = list()
message = deque(message)
while len(message) >= 2:
if message[0] != message[1]:
two_words.append(message.popleft())
two_words.append(message.popleft())
else:
if message[0] != 'X':
two_words.append(message.popleft()+'X')
else:
two_words.append(message.popleft()+'Q')
if message:
two_words.append(message[0]+'X')
message = ''.join(two_words)
two_words = list()
for i in range(0,len(message),2):
two_words.append(message[i:i+2])
return two_words
인풋 메시지로 두 글자 암호화된 리스트 반환
string 형태로 반환해도 되지만 뒤의 함수에서 편리한 재사용을 위해 string list로 반환함.
Ex) HELLOWORLD > ['HE', 'LX', 'LO', 'WO', 'RL', 'DX']
def amhohwa(key):
table = [[0]*5 for _ in range(5)]
overlap = set()
alpha = set()
for i in range(65,91):
if i == 74:
continue
alpha.add(chr(i))
temp_list = list()
for k in key:
if k not in overlap:
overlap.add(k)
temp_list.append(k)
temp_list.extend(sorted(list(alpha-overlap)))
temp_list = deque(temp_list)
for i in range(5):
for j in range(5):
table[i][j] = temp_list.popleft()
return table
키를 통해서 그림과 같은 테이블을 반환하는 함수
def amhohwa2(two_words,table):
answer = []
for word in two_words:
for i in range(5):
for j in range(5):
if table[i][j] == word[0]:
a,b = i,j
if table[i][j] == word[1]:
c,d = i,j
# Case 1
if a == c:
if b != 4 and d != 4:
answer.append(table[a][b+1]+table[c][d+1])
elif b == 4 and d != 4:
answer.append(table[a][0]+table[c][d+1])
elif b!=4 and d == 4:
answer.append(table[a][b+1]+table[c][0])
else:
answer.append(table[a][0]+table[c][0])
# Case 2
elif b == d:
if a != 4 and c != 4:
answer.append(table[a+1][b]+table[c+1][d])
elif a == 4 and c != 4:
answer.append(table[0][b]+table[c+1][d])
elif a != 4 and c == 4:
answer.append(table[a+1][b]+table[0][d])
else:
answer.append(table[0][b]+table[0][d])
# Case 3
else:
answer.append(table[a][d]+table[c][b])
return answer
두 글자의 x,y 위치를 받아와서 문제에서 주어진 조건에 맞게 구현했다.
case 1 > 행이 같을 때
case 2 > 열이 같을 때
case 3 > 그 외
import sys
from collections import deque
def devide(message):
two_words = list()
message = deque(message)
while len(message) >= 2:
if message[0] != message[1]:
two_words.append(message.popleft())
two_words.append(message.popleft())
else:
if message[0] != 'X':
two_words.append(message.popleft()+'X')
else:
two_words.append(message.popleft()+'Q')
if message:
two_words.append(message[0]+'X')
message = ''.join(two_words)
two_words = list()
for i in range(0,len(message),2):
two_words.append(message[i:i+2])
return two_words
def amhohwa(key):
table = [[0]*5 for _ in range(5)]
overlap = set()
alpha = set()
for i in range(65,91):
if i == 74:
continue
alpha.add(chr(i))
temp_list = list()
for k in key:
if k not in overlap:
overlap.add(k)
temp_list.append(k)
temp_list.extend(sorted(list(alpha-overlap)))
temp_list = deque(temp_list)
for i in range(5):
for j in range(5):
table[i][j] = temp_list.popleft()
return table
def amhohwa2(two_words,table):
answer = []
for word in two_words:
for i in range(5):
for j in range(5):
if table[i][j] == word[0]:
a,b = i,j
if table[i][j] == word[1]:
c,d = i,j
# Case 1
if a == c:
if b != 4 and d != 4:
answer.append(table[a][b+1]+table[c][d+1])
elif b == 4 and d != 4:
answer.append(table[a][0]+table[c][d+1])
elif b!=4 and d == 4:
answer.append(table[a][b+1]+table[c][0])
else:
answer.append(table[a][0]+table[c][0])
# Case 2
elif b == d:
if a != 4 and c != 4:
answer.append(table[a+1][b]+table[c+1][d])
elif a == 4 and c != 4:
answer.append(table[0][b]+table[c+1][d])
elif a != 4 and c == 4:
answer.append(table[a+1][b]+table[0][d])
else:
answer.append(table[0][b]+table[0][d])
# Case 3
else:
answer.append(table[a][d]+table[c][b])
return answer
message = input()
key = input()
two_words = devide(message)
table = amhohwa(key)
answer = amhohwa2(two_words,table)
print(''.join(answer))