Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Keys and Locks by gm86xx
def keys_and_locks(lock, some_key):
def trim_zero(s):
m = s.strip().split()
nonzero_row = [i for i in range(len(m)) if any(m[i][j]!='0' for j in range(len(m[0])))]
nonzero_col = [j for j in range(len(m[0])) if any(m[i][j]!='0' for i in range(len(m)))]
return [[m[i][j] for j in nonzero_col] for i in nonzero_row]
def rot90(m):
return [[m[j][len(m[0])-i-1] for j in range(len(m))] for i in range(len(m[0]))]
lock = trim_zero(lock)
key = trim_zero(some_key)
key90 = rot90(key)
key180 = rot90(key90)
key270 = rot90(key180)
return any(lock==k for k in (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
assert keys_and_locks('''
###0
0#00
0000''',
'''
##00
##00''') == False
print("Coding complete? Click 'Check' to earn cool rewards!")
Nov. 9, 2018
Comments: