Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Matrices, use of index, max() and abs() solution in Clear category for Compass, Map and Spyglass by MarcosRecio
def navigation(matrix):
for n in matrix:
for i in ['Y','C','M','S']:
if i in n:
if i == 'Y':
Yx, Yy = n.index(i), matrix.index(n)
elif i == 'C':
Cx, Cy = n.index(i), matrix.index(n)
elif i == 'M':
Mx, My = n.index(i), matrix.index(n)
elif i == 'S':
Sx, Sy = n.index(i), matrix.index(n)
Yc = max(abs(Yx-Cx), abs(Yy-Cy))
Ym = max(abs(Yx-Mx), abs(Yy-My))
Ys = max(abs(Yx-Sx), abs(Yy-Sy))
return Yc+Ym+Ys
if __name__ == '__main__':
print("Example:")
print(navigation([['Y', 0, 0, 0, 'C'],
[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0],
['M', 0, 0, 0, 'S']]))
#These "asserts" using only for self-checking and not necessary for auto-testing
assert navigation([['Y', 0, 0, 0, 'C'],
[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0],
['M', 0, 0, 0, 'S']]) == 11
assert navigation([[ 0, 0, 'C'],
[ 0, 'S', 0],
['M','Y', 0]]) == 4
assert navigation([[ 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 'M', 0, 'S', 0],
[ 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 'C', 0, 0, 0],
[ 0, 'Y',0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0]]) == 9
print("Coding complete? Click 'Check' to earn cool rewards!")
May 23, 2019
Comments: