Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions 17주차/(P)60059/60059_python_kimjoohye.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import numpy as np
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

프로그래머스에서는 numpy 라이브러리가 사용 가능한가보네요! 외부 라이브러리라서, 기업 코테를 준비하신다면 numpy 없이 문제를 풀어보시는 것도 추천드립니다


#key와 lock의 각각의 요소를 합친 배열의 결과 - answer
#answer의 배열의 요소가 모두 1이 아니면 false
def checklock(answer):
for i in answer:
for j in i:
if j!=1:
return False
return True
Comment on lines +5 to +10
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

자주 쓰는 코드묶음을 함수화 해두는 것은 나중에 디버깅할 때에도 훨씬 편하겠죠 👍🏼


#lock을 key의 size-1만큼 더해서 확장시킨 배열로 만들고 가장자리부분 0으로 초기화
#key와 lock의 블록이 하나씩, 두개씩, 세개씩, ... 점점 많아지게 겹쳐서 더한 후
#(겹쳐지는 부분이 많아지다가 모든 key요소와 모든 lock의 요소가 합쳐진 다음 다시 겹쳐지는 부분이 줄어듬)
#원래의 lock size에 맞게 배열 slice - checklock함수로 unlock할 수 있는지 확인
def unlock(answerkey, lock):
N = len(lock)
M = len(answerkey)
#확장
lock = np.pad(lock, (M-1, M-1))

#확장시킨 배열에 key를 오른쪽 아래방향으로 한칸씩 옮겨가면서 더하고 원래의 lock size에 맞게 배열 slice
newM = N+M-1
for i in range(newM):
for j in range(newM):
resultlock = lock.copy()
resultlock[i:i+M,j:j+M] = lock[i:i+M,j:j+M] + answerkey[:][:]
if checklock(resultlock[M-1:M+N-1,M-1:M+N-1]):
return True

return False
def solution(key, lock):
#numpy array로 변경
answerkey = np.array(key)
lock = np.array(lock)

flag = False

for i in range(4):
# i*90만큼 key회전 -> 0, 90, 180, 270만 수행
answerkey = np.rot90(answerkey, i)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

회전은 list(zip(*arr[::-1]))로 90도 회전할 수 있습니다!

flag = unlock(answerkey, lock)
if flag == True:
break
return flag