https://programmers.co.kr/learn/courses/30/lessons/60059
def rotating(key):
# left rotating
length = len(key)
tmp = [[0for _ in range(length)] for _ in range(length)]
for i in range(length):
for j in range(length):
tmp[length-1-j][i] = key[i][j]
return tmp
def trying(n,m,key,board):
for i in range(n, len(key) +n):
for j in range(m, len(key) + m):
board[i][j] = board[i][j] + key[i-n][j-m]
return None
def reset(n,m,key,board):
for i in range(n, len(key) +n):
for j in range(m, len(key) + m):
board[i][j] = board[i][j] - key[i-n][j-m]
return None
def checking(n,m,key,board,lock):
for i in range(len(key)-1, len(key)-1+len(lock)):
for j in range(len(key)-1, len(key)-1+len(lock)):
if board[i][j] != 1:
return False
else:
return True
return None
def solution(key, lock):
answer = False
tmp = 0
key_length = len(key)
lock_length = len(lock)
board = [[0 for _ in range((key_length-1) * 2 + lock_length)] for _ in range((key_length-1) * 2 + lock_length)]
for i in range(key_length, key_length + lock_length):
for j in range(key_length, key_length + lock_length):
board[i-1][j-1] = lock[i-key_length][j-key_length]
board_length = len(board)
for cnt in range(4):
key= rotating(key)
for i in range(board_length - key_length+1):
for j in range(board_length - key_length+1):
trying(i,j,key,board)
flag=checking(i,j,key,board,lock)
if flag == True:
return True
elif flag == False:
reset(i,j,key,board)
return answer