-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7f0ae7c
commit dda16ac
Showing
2 changed files
with
55 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
def solution(key, lock): | ||
def rotate_matrix(rkey): | ||
return list(zip(*rkey[::-1])) | ||
|
||
def attach(x, y, rkey): | ||
for i in range(len(key)): | ||
for j in range(len(key)): | ||
extended_lock[x+i][y+j] += rkey[i][j] | ||
|
||
def detach(i, j, rkey): | ||
for i in range(len(key)): | ||
for j in range(len(key)): | ||
extended_lock[x+i][y+j] -= rkey[i][j] | ||
|
||
def check(): | ||
for i in range(len(lock)): | ||
for j in range(len(lock)): | ||
if extended_lock[i+key_size][j+key_size] != 1: | ||
return False | ||
return True | ||
|
||
|
||
key_size = len(key) | ||
size = len(lock) + 2 * key_size | ||
extended_lock = [[0] * size for _ in range(size)] | ||
|
||
for i in range(len(lock)): | ||
for j in range(len(lock)): | ||
extended_lock[i+key_size][j+key_size] = lock[i][j] | ||
|
||
for i in range(len(extended_lock) - key_size + 1): | ||
for j in range(len(extended_lock) - key_size + 1): | ||
rotated_key = key | ||
for k in range(4): | ||
rotated_key = rotate_matrix(rotated_key) | ||
attach(i, j, rotated_key) | ||
if check(): | ||
return True | ||
detach(i, j, rotated_key) | ||
|
||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
import copy | ||
matrix = [[1,2,3], [4,5,6],[7,8,9]] | ||
|
||
col = len(matrix[0]) | ||
new_matrix = [[0] * col for x in matrix] | ||
def rotate_matrix90(matrix): | ||
new_matrix = [[0] * len(matrix) for _ in range(len(matrix))] | ||
col = len(matrix[0]) | ||
for i in range(len(matrix)): | ||
for j in range(len(matrix[0])): | ||
new_matrix[i][j] = matrix[col-j-1][i] | ||
return new_matrix | ||
|
||
for i in range(len(matrix)): | ||
for j in range(len(matrix[0])): | ||
new_matrix[i][j] = matrix[col-j-1][i] | ||
matrix = new_matrix[:] | ||
|
||
for i in range(len(matrix)): | ||
for j in range(len(matrix[0])): | ||
new_matrix[i][j] = matrix[col-j-1][i] | ||
|
||
|
||
print(new_matrix) | ||
|
||
def cool_rotate_matrix90(matrix): | ||
return list(zip(*matrix)) | ||
|
||
|
||
print(rotate_matrix90(matrix)) | ||
print(cool_rotate_matrix90(matrix[::-1])) |