Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Matrix manipulation solution in Clear category for Keys and Locks by kumaus
def strip(shape):
"""
Remove superfluous padding zeroes in shape, where shape is a string vector:
- Remove all zero rows
- transpose the matrix using zip()
- remove all zero rows in transposed matrix
The return value is a transpose of the original shape as a single character matrix,
which is irrelevant because the same is done to lock and key
"""
strip_rows = [row for row in shape if '#' in row]
return [col for col in zip(*strip_rows) if '#' in col]
def keys_and_locks(lock, some_key):
"""
First, superfluous padding zeroes are removed from lock and key.
Then, identity between lock and key is checked for keys rotated through 90°, 180°, 270° and 360°
"""
some_key = strip(some_key.split())
lock = strip(lock.split())
for rot in range(4):
# rotate key through 90°
some_key = [row[::-1] for row in zip(*some_key)]
if some_key == lock:
return True
return False
Dec. 14, 2018
Comments: