https://softeer.ai/practice/info.do?eventIdx=1&psProblemId=804
Softeer
연습문제를 담을 Set을 선택해주세요. 취소 확인
softeer.ai

풀이
문제를 보았을때 알수있는 힌트
조건이 많아서 어떻게 시작을 해야할까 고민하면서 시작을 하게 되었다.
먼저 5x5의 그래프를 만드는 것을 먼저 하였다.
조건에 나와있듯이 같은 단어는 제외를 시키고 처음부터 하나씩 적고 빈칸은 남은 알파벳을 넣으면 된다.
set을 통해서 같은 알파벳을 제외 시키고 남은 알파벳을 넣어 주었다. 그리고 graph 리스트를 만들어 놓았다.
행열이 바뀐 row_graph 도 만들어 놓았다.
다음으로는 메세지의 글을 2글자씩 만드는 작업을 진행하였다.
리스트에서 pop을 하는 형식으로 빼주면서 계산을 하였다. 겹치는 단어가 있으면 X를 X가 같이 있는 경우는 Q를 마지막 단어가 X의 경우만 예외로 XX가 가능하게 끔 작업을 하였다. 이렇게 만든 코드를 n_list에 담아주었다.
이제 n_list를 graph에 3단계 조건에 성립 되게 하면 끝이난다.
먼저 첫 알파벳이 나오는 x,y 위치를 찾고 1단계는 같은 행 이기에 X만을 참고한다.
두번째 알파벳이 있다면 한칸씩 더하여 n_list 값을 변경한다.
없다면 다음 조건으로 넘어간다.
다음 조건은 row_graph를 이용하여 위 방식을 똑같이 하면 된다. 물론 y를 를 기준으로 하면 된다.
여기에도 조건이 겹치지 않는 다면
전체적으로 하나씩 확인하며 x,y줄이 아닌 경우만 확인하고 위치를 변경하면 끝이난다.
다른 사람들의 코드도 확인하고 싶은데 올린 블로그 글이 없어서 더 효율적인 코드는 짜지 못해서 아쉽다..
import sys
input= sys.stdin.readline
al="abcdefghiklmnopqrstuvwxyz"
n=list(input().strip())
m=set(input().strip())
for i in al:
if i.upper() not in m:
m.add(i.upper())
# print(n,m)
m=list(m)
graph=[]
for i in range(5):
temp=[]
for j in range(5):
temp.append(m[i*5+j])
graph.append(temp)
row_graph = list(map(list, zip(*graph)))
n_list=[]
while n:
if len(n)==1:
n_list.append([n[0],"X"])
n.pop(0)
else:
if n[0]==n[1]:
if n[0]=="X":
n_list.append([n[0],"Q"])
else:
n_list.append([n[0],"X"])
n.pop(0)
else:
n_list.append([n[0],n[1]])
n.pop(0)
n.pop(0)
for i in range(len(n_list)):
temp=[]
for j in range(5):
if n_list[i][0] in graph[j]:
temp=[j,graph[j].index(n_list[i][0])]
if n_list[i][1] in graph[temp[0]]:
n_list[i][0] = graph[temp[0]][(graph[temp[0]].index(n_list[i][0])+1)%5]
n_list[i][1] = graph[temp[0]][(graph[temp[0]].index(n_list[i][1])+1)%5]
elif n_list[i][1] in row_graph[temp[1]]:
n_list[i][0] = row_graph[temp[1]][(row_graph[temp[1]].index(n_list[i][0])+1)%5]
n_list[i][1] = row_graph[temp[1]][(row_graph[temp[1]].index(n_list[i][1])+1)%5]
else:
for j in range(5):
for k in range(5):
if graph[j][k]==n_list[i][1]:
if j!=temp[0] and k!=temp[1]:
n_list[i][0]= graph[temp[0]][k]
n_list[i][1]= graph[j][temp[1]]
for i in n_list:
for j in i:
print(j,end="")
https://github.com/dydwkd486/coding_test/blob/main/softeer/플레이페어.py
GitHub - dydwkd486/coding_test: 코딩테스트 공부한 내용 정리
코딩테스트 공부한 내용 정리. Contribute to dydwkd486/coding_test development by creating an account on GitHub.
github.com
'Coding Test > softeer' 카테고리의 다른 글
[소프티어] 스마트 물류- python (레벨 3) (0) | 2022.05.13 |
---|---|
[소프티어] 성격 평균- python (레벨 3) (0) | 2022.05.10 |
[소프티어] 징검다리- python (레벨 3) (0) | 2022.04.30 |
[소프티어] 동계 테스트 시점 예측 - python (레벨 3) (0) | 2022.04.28 |
[소프티어] 조립라인 - python (레벨 3) (0) | 2022.04.28 |