Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Weak Point solution in Clear category for Weak Point by colinmcnicholl
def weak_point(matrix):
"""Input: A durability map as a list of lists with integers.
Function finds the co-ordinate of the intersection of the weakest row
and column in the matrix. Weakest row (column) is row (column) for
which the sum of the durability measurements is the smallest. If ties
choose top left.
Output: The coordinates of the weak point as a list (tuple) of integers.
"""
row_sums = [sum(row) for row in matrix]
row_index = row_sums.index(min(row_sums))
transpose_matrix = zip(*matrix)
column_sums = [sum(col) for col in transpose_matrix]
col_index = column_sums.index(min(column_sums))
return [row_index, col_index]
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert isinstance(weak_point([[1]]), (list, tuple)), "The result should be a list or a tuple"
assert list(weak_point([[7, 2, 7, 2, 8],
[2, 9, 4, 1, 7],
[3, 8, 6, 2, 4],
[2, 5, 2, 9, 1],
[6, 6, 5, 4, 5]])) == [3, 3], "Example"
assert list(weak_point([[7, 2, 4, 2, 8],
[2, 8, 1, 1, 7],
[3, 8, 6, 2, 4],
[2, 5, 2, 9, 1],
[6, 6, 5, 4, 5]])) == [1, 2], "Two weak point"
assert list(weak_point([[1, 1, 1],
[1, 1, 1],
[1, 1, 1]])) == [0, 0], "Top left"
Dec. 17, 2017