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 PascalFirley
def rotate(key): #rotate the key 90°anti-clockwise
r=[]
b=len(key)
h=len(key[0])
for x in range(h):
l=""
for y in range(b):
l+=key[-y-1][x]
r.append(l)
return(r)
def toMinimal(m): #remove all the lines with only '0'
for a in range(4):
while not "#" in m[0]:
del m[0]
while not "#" in m[-1]:
del m[-1]
m=rotate(m)
return m
def keys_and_locks(lock, some_key):
l=lock.lstrip().split("\n") #make an array of the lock
l=toMinimal(l) #and remove the empty lines
some_key=some_key.lstrip()
k=some_key.split("\n") #make an array of the key
k=toMinimal(k) #and remove the empty lines
for a in range(4):
k=rotate(k) #rotate the key
if k==l: #check if the key fits the lock
return True
return False
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!")
Feb. 2, 2019
Comments: