Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Weak Point by Rounin
import functools, math
def weak_point(matrix):
rowsums = []
colsums = []
for row in matrix:
rowsums.append(sum(row))
for i in range(len(row)):
if i >= len(colsums):
colsums.append(0)
colsums[i] += row[i]
rows = []
cols = []
for i in range(len(matrix)):
rows.append(i)
for i in range(len(matrix[0])):
cols.append(i)
rows = sorted(rows, key = functools.cmp_to_key(lambda a, b: (a-b if rowsums[a] == rowsums[b] else rowsums[a] - rowsums[b])))
cols = sorted(cols, key = functools.cmp_to_key(lambda a, b: (a-b if colsums[a] == colsums[b] else colsums[a] - colsums[b])))
return [rows[0], cols[0]]
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"
Sept. 29, 2017