문제

코드
def rotate_a_matrix_by_90_degree(a: list) -> list:
n = len(a)
m = len(a[0])
result = [[0] * n for _ in range(m)]
for i in range(n):
for j in range(m):
result[j][n-i-1] = a[i][j]
return result
def check(new_lock) -> bool:
lock_length = len(new_lock) // 3
for i in range(lock_length, lock_length*2):
for j in range(lock_length, lock_length*2):
if new_lock[i][j] != 1:
return False
return True
def solution(key: list, lock: list) -> bool:
n = len(lock)
m = len(key)
new_lock = [[0] * (n*3) for _ in range(n*3)]
for i in range(n):
for j in range(n):
new_lock[i+n][j+n] = lock[i][j]
for _ in range(4):
key = rotate_a_matrix_by_90_degree(key)
for x in range(n*2):
for y in range(n*2):
for i in range(m):
for j in range(m):
new_lock[x+i][y+j] += key[i][j]
if check(new_lock) == True:
return True
for i in range(m):
for j in range(m):
new_lock[x+i][y+j] -= key[i][j]
return False
if __name__ == '__main__':
print(solution([[0,0,0], [1,0,0], [0,1,1]], [[1, 1, 1], [1, 1, 0], [1, 0, 1]]))
출처 & 깃허브
programmers 자물쇠와 열쇠
이것이 취업을 위한 코딩 테스트다 with python
github