Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First_colin_crop_rotate_keys_and_locks solution in Clear category for Keys and Locks by colinmcnicholl
import operator
def crop(a_string, min_row, max_row, min_col, max_col):
lines = a_string.split()
b_string = ''
for i in range(min_row, max_row + 1):
for j in range(min_col, max_col + 2):
if j == max_col + 1:
b_string += '\n'
else:
b_string += lines[i][j]
return b_string
def borders(a_string):
lines = a_string.split()
coords = []
for i in range(len(lines)):
for j in range(len(lines[0])):
if lines[i][j] == '#':
coords.append((i, j))
min_col = min(coords, key=operator.itemgetter(1))[1]
max_col = max(coords, key=operator.itemgetter(1))[1]
min_row = min(coords, key=operator.itemgetter(0))[0]
max_row = max(coords, key=operator.itemgetter(0))[0]
return min_row, max_row, min_col, max_col
def rotate(key):
array = []
lines = key.split()
for line in lines:
chars = list(line)
array.append(chars)
rotated = list(zip(*array[::-1]))
key90 = ''
for elem in rotated:
line = "".join(elem)
key90 += line + '\n'
return key90
def keys_and_locks(lock, some_key):
min_row, max_row, min_col, max_col = borders(lock)
_lock = crop(lock, min_row, max_row, min_col, max_col)
min_row, max_row, min_col, max_col = borders(some_key)
_some_key = crop(some_key, min_row, max_row, min_col, max_col)
key90 = rotate(_some_key)
key180 = rotate(key90)
key270 = rotate(key180)
return any([key == _lock for key in [_some_key, key90, key180, key270]])
if __name__ == '__main__':
print("Example:")
print(keys_and_locks('''
0##0
0##0
00#0
00##
00##''',
'''
00000
000##
#####
##000
00000'''))
#These "asserts" using only for self-checking and not necessary for auto-testing
assert keys_and_locks('''
0##0
0##0
00#0
00##
00##''',
'''
00000
000##
#####
##000
00000''') == True
assert keys_and_locks('''
###0
00#0''',
'''
00000
00000
#0000
###00
0#000
0#000''') == False
assert keys_and_locks('''
0##0
0#00
0000''',
'''
##000
#0000
00000
00000
00000''') == True
print("Coding complete? Click 'Check' to earn cool rewards!")
Sept. 13, 2018