You are given a matrix, consisting of three rows and three columns, represented as an array A of nine integers. The rows of the matrix are numbered from 0 to 2 (from top to bottom) and the columns are numbered from 0 to 2 (from left to right). The matrix element in the J-th row and K-th column corresponds to the array element A[J*3 + K]. For example, the matrix below corresponds to array [0, 2, 3, 4, 1, 1, 1, 3, 1].
In one move you can increment any element by 1.
Your task is to find a matrix whose elements in each row and each column sum to an equal value, which can be constructed from the given matrix in a minimal number of moves.
Write a function:
def solution(A)
that, given an array A of nine integers, returns an array of nine integers, representing the matrix described above. If there are several possible answers, the function may return any of them.
Examples:
1. Given A = [0, 2, 3, 4, 1, 1, 1, 3, 1], the function could return [1, 2, 3, 4, 1, 1, 1, 3, 2]. The sum of elements in each row and each column of the returned matrix is 6. Two increments by 1 are enough. You can increment A[0] and A[8] (top-left and bottom-right matrix elements). This gives [1, 2, 3, 4, 1, 1, 1, 3, 2], which satisfies the statement's conditions. Alternatively, you can increment A[2] and A[6] (top-right and bottom-left matrix elements). This gives another correct solution: [0, 2, 4, 4, 1, 1, 2, 3, 1].
2. Given A = [1, 1, 1, 2, 2, 1, 2, 2, 1], the function should return [1, 1, 3, 2, 2, 1, 2, 2, 1]. The sum of elements in each row and each column of the returned matrix is 5. Two increments by 1 are enough. You can increment A[2] (top-right matrix element) twice. In this case, there are no other correct solutions.
Write an efficient algorithm for the following assumptions:
- array A contains nine elements;
- each element of array A is an integer within the range [0..100,000,000].
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
pass
abc = A[0] + A[1] + A[2]
dwf = A[3] + A[4] + A[5]
ghi = A[6] + A[7] + A[8]
adg = A[0] + A[3] + A[6]
beh = A[1] + A[4] + A[7]
cfi = A[2] + A[5] + A[8]
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
abc = A[0] + A[1] + A[2]
dwf = A[3] + A[4] + A[5]
ghi = A[6] + A[7] + A[8]
adg = A[0] + A[3] + A[6]
beh = A[1] + A[4] + A[7]
cfi = A[2] + A[5] + A[8]
avg = sum(abc + dwf + ghi + adg + beh + cfi) / 6
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
abc = A[0] + A[1] + A[2]
dwf = A[3] + A[4] + A[5]
ghi = A[6] + A[7] + A[8]
adg = A[0] + A[3] + A[6]
beh = A[1] + A[4] + A[7]
cfi = A[2] + A[5] + A[8]
total = sum(abc + dwf + ghi + adg + beh + cfi) / 6
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
abc = A[0] + A[1] + A[2]
dwf = A[3] + A[4] + A[5]
ghi = A[6] + A[7] + A[8]
adg = A[0] + A[3] + A[6]
beh = A[1] + A[4] + A[7]
cfi = A[2] + A[5] + A[8]
total = sum(abc + dwf + ghi + adg + beh + cfi)
target = int(total / 6) + 1 if total % 6 else total // 6
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6\
abc = A[0] + A[1] + A[2]
dwf = A[3] + A[4] + A[5]
ghi = A[6] + A[7] + A[8]
adg = A[0] + A[3] + A[6]
beh = A[1] + A[4] + A[7]
cfi = A[2] + A[5] + A[8]
total = sum(abc + dwf + ghi + adg + beh + cfi)
target = int(total / 6) + 1 if total % 6 else total // 6
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
def row(i):
return A[i * 3] + A[i * 3 + 1] + A[i * 3 + 2]
def col(i):
return A[i] + A[(i + 3) % 9]
abc = A[0] + A[1] + A[2]
dwf = A[3] + A[4] + A[5]
ghi = A[6] + A[7] + A[8]
adg = A[0] + A[3] + A[6]
beh = A[1] + A[4] + A[7]
cfi = A[2] + A[5] + A[8]
total = sum(abc + dwf + ghi + adg + beh + cfi)
target = int(total / 6) + 1 if total % 6 else total // 6
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
def row(i):
return A[i * 3] + A[i * 3 + 1] + A[i * 3 + 2]
def col(i):
return A[i] + A[i + 3] + A[i + 6]
abc = A[0] + A[1] + A[2]
dwf = A[3] + A[4] + A[5]
ghi = A[6] + A[7] + A[8]
adg = A[0] + A[3] + A[6]
beh = A[1] + A[4] + A[7]
cfi = A[2] + A[5] + A[8]
total = sum(abc + dwf + ghi + adg + beh + cfi)
target = int(total / 6) + 1 if total % 6 else total // 6
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
def row(i):
return A[i * 3] + A[i * 3 + 1] + A[i * 3 + 2]
def col(i):
return A[i] + A[i + 3] + A[i + 6]
abc = A[0] + A[1] + A[2]
dwf = A[3] + A[4] + A[5]
ghi = A[6] + A[7] + A[8]
adg = A[0] + A[3] + A[6]
beh = A[1] + A[4] + A[7]
cfi = A[2] + A[5] + A[8]
total = sum(abc + dwf + ghi + adg + beh + cfi)
target = int(total / 6) + 1 if total % 6 else total // 6
A[0] += min(target - row(0), target - col(0))
A[1] += min(target - row(0), target - col(1))
A[2] += min(target - row(0), target - col(2))
A[3] += min(target - row(0), target - col(0))
A[4] += min(target - row(0), target - col(1))
A[5] += min(target - row(0), target - col(2))
A[6] += min(target - row(0), target - col(0))
A[7] += min(target - row(0), target - col(1))
A[8] += min(target - row(0), target - col(2))
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
def row(i):
return A[i * 3] + A[i * 3 + 1] + A[i * 3 + 2]
def col(i):
return A[i] + A[i + 3] + A[i + 6]
abc = A[0] + A[1] + A[2]
dwf = A[3] + A[4] + A[5]
ghi = A[6] + A[7] + A[8]
adg = A[0] + A[3] + A[6]
beh = A[1] + A[4] + A[7]
cfi = A[2] + A[5] + A[8]
total = sum(abc + dwf + ghi + adg + beh + cfi)
target = int(total / 6) + 1 if total % 6 else total // 6
A[0] += min(target - row(0), target - col(0))
A[1] += min(target - row(0), target - col(1))
A[2] += min(target - row(0), target - col(2))
A[3] += min(target - row(1), target - col(0))
A[4] += min(target - row(1), target - col(1))
A[5] += min(target - row(1), target - col(2))
A[6] += min(target - row(2), target - col(0))
A[7] += min(target - row(2), target - col(1))
A[8] += min(target - row(2), target - col(2))
return A
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
def row(i):
return A[i * 3] + A[i * 3 + 1] + A[i * 3 + 2]
def col(i):
return A[i] + A[i + 3] + A[i + 6]
abc = A[0] + A[1] + A[2]
dwf = A[3] + A[4] + A[5]
ghi = A[6] + A[7] + A[8]
adg = A[0] + A[3] + A[6]
beh = A[1] + A[4] + A[7]
cfi = A[2] + A[5] + A[8]
total = sum(abc + dwf + ghi + adg + beh + cfi)
target = int(total / 6) + 1 if total % 6 else total // 6
A[0] += min(target - row(0), target - col(0))
A[1] += min(target - row(0), target - col(1))
A[2] += min(target - row(0), target - col(2))
A[3] += min(target - row(1), target - col(0))
A[4] += min(target - row(1), target - col(1))
A[5] += min(target - row(1), target - col(2))
A[6] += min(target - row(2), target - col(0))
A[7] += min(target - row(2), target - col(1))
A[8] += min(target - row(2), target - col(2))
return A
Traceback (most recent call last): File "exec.py", line 141, in <module> main() File "exec.py", line 103, in main result = solution( A ) File "/tmp/solution.py", line 16, in solution total = sum(abc + dwf + ghi + adg + beh + cfi) TypeError: 'int' object is not iterable
Traceback (most recent call last): File "exec.py", line 141, in <module> main() File "exec.py", line 103, in main result = solution( A ) File "/tmp/solution.py", line 16, in solution total = sum(abc + dwf + ghi + adg + beh + cfi) TypeError: 'int' object is not iterable
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
def row(i):
return A[i * 3] + A[i * 3 + 1] + A[i * 3 + 2]
def col(i):
return A[i] + A[i + 3] + A[i + 6]
abc = A[0] + A[1] + A[2]
dwf = A[3] + A[4] + A[5]
ghi = A[6] + A[7] + A[8]
adg = A[0] + A[3] + A[6]
beh = A[1] + A[4] + A[7]
cfi = A[2] + A[5] + A[8]
total = abc + dwf + ghi + adg + beh + cfi
target = int(total / 6) + 1 if total % 6 else total // 6
A[0] += min(target - row(0), target - col(0))
A[1] += min(target - row(0), target - col(1))
A[2] += min(target - row(0), target - col(2))
A[3] += min(target - row(1), target - col(0))
A[4] += min(target - row(1), target - col(1))
A[5] += min(target - row(1), target - col(2))
A[6] += min(target - row(2), target - col(0))
A[7] += min(target - row(2), target - col(1))
A[8] += min(target - row(2), target - col(2))
return A
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
def row(i):
return A[i * 3] + A[i * 3 + 1] + A[i * 3 + 2]
def col(i):
return A[i] + A[i + 3] + A[i + 6]
abc = A[0] + A[1] + A[2]
dwf = A[3] + A[4] + A[5]
ghi = A[6] + A[7] + A[8]
adg = A[0] + A[3] + A[6]
beh = A[1] + A[4] + A[7]
cfi = A[2] + A[5] + A[8]
total =
total = abc + dwf + ghi + adg + beh + cfi
target = int(total / 6) + 1 if total % 6 else total // 6
A[0] += min(target - row(0), target - col(0))
A[1] += min(target - row(0), target - col(1))
A[2] += min(target - row(0), target - col(2))
A[3] += min(target - row(1), target - col(0))
A[4] += min(target - row(1), target - col(1))
A[5] += min(target - row(1), target - col(2))
A[6] += min(target - row(2), target - col(0))
A[7] += min(target - row(2), target - col(1))
A[8] += min(target - row(2), target - col(2))
return A
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
def row(i):
return A[i * 3] + A[i * 3 + 1] + A[i * 3 + 2]
def col(i):
return A[i] + A[i + 3] + A[i + 6]
abc = A[0] + A[1] + A[2]
dwf = A[3] + A[4] + A[5]
ghi = A[6] + A[7] + A[8]
adg = A[0] + A[3] + A[6]
beh = A[1] + A[4] + A[7]
cfi = A[2] + A[5] + A[8]
total = sum(row(i) + col(i) for i in range(3))
target = int(total / 6) + 1 if total % 6 else total // 6
A[0] += min(target - row(0), target - col(0))
A[1] += min(target - row(0), target - col(1))
A[2] += min(target - row(0), target - col(2))
A[3] += min(target - row(1), target - col(0))
A[4] += min(target - row(1), target - col(1))
A[5] += min(target - row(1), target - col(2))
A[6] += min(target - row(2), target - col(0))
A[7] += min(target - row(2), target - col(1))
A[8] += min(target - row(2), target - col(2))
return A
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
def row(i):
return A[i * 3] + A[i * 3 + 1] + A[i * 3 + 2]
def col(i):
return A[i] + A[i + 3] + A[i + 6]
total = sum(row(i) + col(i) for i in range(3))
target = int(total / 6) + 1 if total % 6 else total // 6
A[0] += min(target - row(0), target - col(0))
A[1] += min(target - row(0), target - col(1))
A[2] += min(target - row(0), target - col(2))
A[3] += min(target - row(1), target - col(0))
A[4] += min(target - row(1), target - col(1))
A[5] += min(target - row(1), target - col(2))
A[6] += min(target - row(2), target - col(0))
A[7] += min(target - row(2), target - col(1))
A[8] += min(target - row(2), target - col(2))
return A
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug messag
e")
def solution(A):
# write your code in Python 3.6
def row(i):
return A[i * 3] + A[i * 3 + 1] + A[i * 3 + 2]
def col(i):
return A[i] + A[i + 3] + A[i + 6]
total = sum(row(i) + col(i) for i in range(3))
target = int(total / 6) + 1 if total % 6 else total // 6
A[0] += min(target - row(0), target - col(0))
A[1] += min(target - row(0), target - col(1))
A[2] += min(target - row(0), target - col(2))
A[3] += min(target - row(1), target - col(0))
A[4] += min(target - row(1), target - col(1))
A[5] += min(target - row(1), target - col(2))
A[6] += min(target - row(2), target - col(0))
A[7] += min(target - row(2), target - col(1))
A[8] += min(target - row(2), target - col(2))
return A
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
def row(i):
return A[i * 3] + A[i * 3 + 1] + A[i * 3 + 2]
def col(i):
return A[i] + A[i + 3] + A[i + 6]
total = sum(row(i) + col(i) for i in range(3))
target = int(total / 6) + 1 if total % 6 else total // 6
A[0] += min(target - row(0), target - col(0))
A[1] += min(target - row(0), target - col(1))
A[2] += min(target - row(0), target - col(2))
A[3] += min(target - row(1), target - col(0))
A[4] += min(target - row(1), target - col(1))
A[5] += min(target - row(1), target - col(2))
A[6] += min(target - row(2), target - col(0))
A[7] += min(target - row(2), target - col(1))
A[8] += min(target - row(2), target - col(2))
return A
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
def row(i):
return A[i * 3] + A[i * 3 + 1] + A[i * 3 + 2]
def col(i):
return A[i] + A[i + 3] + A[i + 6]
total = sum(row(i) + col(i) for i in range(3))
# target the average of the rows (rounding up since we can't decrement)
# this should allow for the fewest number of changes
target = int(total / 6) + 1 if total % 6 else total // 6
A[0] += min(target - row(0), target - col(0))
A[1] += min(target - row(0), target - col(1))
A[2] += min(target - row(0), target - col(2))
A[3] += min(target - row(1), target - col(0))
A[4] += min(target - row(1), target - col(1))
A[5] += min(target - row(1), target - col(2))
A[6] += min(target - row(2), target - col(0))
A[7] += min(target - row(2), target - col(1))
A[8] += min(target - row(2), target - col(2))
return A
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
def row(i):
return A[i * 3] + A[i * 3 + 1] + A[i * 3 + 2]
def col(i):
return A[i] + A[i + 3] + A[i + 6]
# target the average of the rows (rounding up since we can't decrement)
# this should allow for the fewest number of increments
total = sum(row(i) + col(i) for i in range(3))
target = int(total / 6) + 1 if total % 6 else total // 6
# increment as much as needed based on row/col constraints
A[0] += min(target - row(0), target - col(0))
A[1] += min(target - row(0), target - col(1))
A[2] += min(target - row(0), target - col(2))
A[3] += min(target - row(1), target - col(0))
A[4] += min(target - row(1), target - col(1))
A[5] += min(target - row(1), target - col(2))
A[6] += min(target - row(2), target - col(0))
A[7] += min(target - row(2), target - col(1))
A[8] += min(target - row(2), target - col(2))
return A
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
def row(i):
return A[i * 3] + A[i * 3 + 1] + A[i * 3 + 2]
def col(i):
return A[i] + A[i + 3] + A[i + 6]
# target the average of the rows (rounding up since we can't decrement)
# this should allow for the fewest number of increments
total = sum(row(i) + col(i) for i in range(3))
target = int(total / 6) + 1 if total % 6 else total // 6
# increment as much as needed based on row/col constraints
A[0] += min(target - row(0), target - col(0))
A[1] += min(target - row(0), target - col(1))
A[2] += min(target - row(0), target - col(2))
A[3] += min(target - row(1), target - col(0))
A[4] += min(target - row(1), target - col(1))
A[5] += min(target - row(1), target - col(2))
A[6] += min(target - row(2), target - col(0))
A[7] += min(target - row(2), target - col(1))
A[8] += min(target - row(2), target - col(2))
return A
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
def row(i):
return A[i * 3] + A[i * 3 + 1] + A[i * 3 + 2]
def col(i):
return A[i] + A[i + 3] + A[i + 6]
# target the average of the rows (rounding up since we can't decrement)
# this should allow for the fewest number of increments
total = sum(row(i) + col(i) for i in range(3))
target = int(total / 6) + 1 if total % 6 else total // 6
# increment as much as needed based on row/col constraints
A[0] += min(target - row(0), target - col(0))
A[1] += min(target - row(0), target - col(1))
A[2] += min(target - row(0), target - col(2))
A[3] += min(target - row(1), target - col(0))
A[4] += min(target - row(1), target - col(1))
A[5] += min(target - row(1), target - col(2))
A[6] += min(target - row(2), target - col(0))
A[7] += min(target - row(2), target - col(1))
A[8] += min(target - row(2), target - col(2))
return A
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
def row(i):
return A[i * 3] + A[i * 3 + 1] + A[i * 3 + 2]
def col(i):
return A[i] + A[i + 3] + A[i + 6]
# target the average of the rows (rounding up since we can't decrement)
# this should allow for the fewest number of increments
target = max([row(0), row(1), row(2), col(0), col(1), col(2)])
#total = sum(row(i) + col(i) for i in range(3))
#target = int(total / 6) + 1 if total % 6 else total // 6
# increment as much as needed based on row/col constraints
A[0] += min(target - row(0), target - col(0))
A[1] += min(target - row(0), target - col(1))
A[2] += min(target - row(0), target - col(2))
A[3] += min(target - row(1), target - col(0))
A[4] += min(target - row(1), target - col(1))
A[5] += min(target - row(1), target - col(2))
A[6] += min(target - row(2), target - col(0))
A[7] += min(target - row(2), target - col(1))
A[8] += min(target - row(2), target - col(2))
return A
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
def row(i):
return A[i * 3] + A[i * 3 + 1] + A[i * 3 + 2]
def col(i):
return A[i] + A[i + 3] + A[i + 6]
# target the largest existing su
# this should allow for the fewest number of increments
target = max([row(0), row(1), row(2), col(0), col(1), col(2)])
#total = sum(row(i) + col(i) for i in range(3))
#target = int(total / 6) + 1 if total % 6 else total // 6
# increment as much as needed based on row/col constraints
A[0] += min(target - row(0), target - col(0))
A[1] += min(target - row(0), target - col(1))
A[2] += min(target - row(0), target - col(2))
A[3] += min(target - row(1), target - col(0))
A[4] += min(target - row(1), target - col(1))
A[5] += min(target - row(1), target - col(2))
A[6] += min(target - row(2), target - col(0))
A[7] += min(target - row(2), target - col(1))
A[8] += min(target - row(2), target - col(2))
return A
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
def row(i):
return A[i * 3] + A[i * 3 + 1] + A[i * 3 + 2]
def col(i):
return A[i] + A[i + 3] + A[i + 6]
# target the largest existing sum as the sum for each row
# this should allow for the fewest number of increments
target = max([row(0), row(1), row(2), col(0), col(1), col(2)])
#total = sum(row(i) + col(i) for i in range(3))
#target = int(total / 6) + 1 if total % 6 else total // 6
# increment as much as needed based on row/col constraints
A[0] += min(target - row(0), target - col(0))
A[1] += min(target - row(0), target - col(1))
A[2] += min(target - row(0), target - col(2))
A[3] += min(target - row(1), target - col(0))
A[4] += min(target - row(1), target - col(1))
A[5] += min(target - row(1), target - col(2))
A[6] += min(target - row(2), target - col(0))
A[7] += min(target - row(2), target - col(1))
A[8] += min(target - row(2), target - col(2))
return A
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
def row(i):
return A[i * 3] + A[i * 3 + 1] + A[i * 3 + 2]
def col(i):
return A[i] + A[i + 3] + A[i + 6]
# target the largest existing sum as the sum for each row
# this should allow for the fewest number of increments
target = max([row(0), row(1), row(2), col(0), col(1), col(2)])
# increment as much as needed based on row/col constraints
A[0] += min(target - row(0), target - col(0))
A[1] += min(target - row(0), target - col(1))
A[2] += min(target - row(0), target - col(2))
A[3] += min(target - row(1), target - col(0))
A[4] += min(target - row(1), target - col(1))
A[5] += min(target - row(1), target - col(2))
A[6] += min(target - row(2), target - col(0))
A[7] += min(target - row(2), target - col(1))
A[8] += min(target - row(2), target - col(2))
return A
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
def row(i):
return A[i * 3] + A[i * 3 + 1] + A[i * 3 + 2]
def col(i):
return A[i] + A[i + 3] + A[i + 6]
# target the largest existing sum as the sum for each row
# this should allow for the fewest number of increments
target = max([row(0), row(1), row(2), col(0), col(1), col(2)])
# increment as much as needed based on row/col constraints
A[0] += min(target - row(0), target - col(0))
A[1] += min(target - row(0), target - col(1))
A[2] += min(target - row(0), target - col(2))
A[3] += min(target - row(1), target - col(0))
A[4] += min(target - row(1), target - col(1))
A[5] += min(target - row(1), target - col(2))
A[6] += min(target - row(2), target - col(0))
A[7] += min(target - row(2), target - col(1))
A[8] += min(target - row(2), target - col(2))
return A
function result: [9, 1, 1, 1, 9, 1, 1, 1, 9]
function result: [9, 1, 1, 1, 9, 1, 1, 1, 9]
[1, 1, 1, 1, 1, 1, 1, 1, 9]
[1, 1, 1, 1, 9, 1, 1, 1, 1]
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
def row(i):
return A[i * 3] + A[i * 3 + 1] + A[i * 3 + 2]
def col(i):
return A[i] + A[i + 3] + A[i + 6]
# target the largest existing sum as the sum for each row
# this should allow for the fewest number of increments
target = max([row(0), row(1), row(2), col(0), col(1), col(2)])
# increment as much as needed based on row/col constraints
A[0] += min(target - row(0), target - col(0))
A[1] += min(target - row(0), target - col(1))
A[2] += min(target - row(0), target - col(2))
A[3] += min(target - row(1), target - col(0))
A[4] += min(target - row(1), target - col(1))
A[5] += min(target - row(1), target - col(2))
A[6] += min(target - row(2), target - col(0))
A[7] += min(target - row(2), target - col(1))
A[8] += min(target - row(2), target - col(2))
return A
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
def row(i):
return A[i * 3] + A[i * 3 + 1] + A[i * 3 + 2]
def col(i):
return A[i] + A[i + 3] + A[i + 6]
# target the largest existing sum as the sum for each row
# this should allow for the fewest number of increments
target = max([row(0), row(1), row(2), col(0), col(1), col(2)])
# increment as much as needed based on row/col constraints
A[0] += min(target - row(0), target - col(0))
A[1] += min(target - row(0), target - col(1))
A[2] += min(target - row(0), target - col(2))
A[3] += min(target - row(1), target - col(0))
A[4] += min(target - row(1), target - col(1))
A[5] += min(target - row(1), target - col(2))
A[6] += min(target - row(2), target - col(0))
A[7] += min(target - row(2), target - col(1))
A[8] += min(target - row(2), target - col(2))
return A
function result: [9, 1, 1, 1, 9, 1, 1, 1, 9]
function result: [10, 1, 1, 1, 9, 2, 1, 2, 9]
[1, 1, 1, 1, 1, 1, 1, 1, 9]
[1, 1, 1, 1, 9, 1, 1, 2, 1]
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
def row(i):
return A[i * 3] + A[i * 3 + 1] + A[i * 3 + 2]
def col(i):
return A[i] + A[i + 3] + A[i + 6]
# target the largest existing sum as the sum for each row
# this should allow for the fewest number of increments
target = max([row(0), row(1), row(2), col(0), col(1), col(2)])
# increment as much as needed based on row/col constraints
A[0] += min(target - row(0), target - col(0))
A[1] += min(target - row(0), target - col(1))
A[2] += min(target - row(0), target - col(2))
A[3] += min(target - row(1), target - col(0))
A[4] += min(target - row(1), target - col(1))
A[5] += min(target - row(1), target - col(2))
A[6] += min(target - row(2), target - col(0))
A[7] += min(target - row(2), target - col(1))
A[8] += min(target - row(2), target - col(2))
return A
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
def row(i):
return A[i * 3] + A[i * 3 + 1] + A[i * 3 + 2]
def col(i):
return A[i] + A[i + 3] + A[i + 6]
# target the largest existing sum as the sum for each row
# this should allow for the fewest number of increments
target = max([row(0), row(1), row(2), col(0), col(1), col(2)])
# increment as much as needed based on row/col constraints
A[0] += min(target - row(0), target - col(0))
A[1] += min(target - row(0), target - col(1))
A[2] += min(target - row(0), target - col(2))
A[3] += min(target - row(1), target - col(0))
A[4] += min(target - row(1), target - col(1))
A[5] += min(target - row(1), target - col(2))
A[6] += min(target - row(2), target - col(0))
A[7] += min(target - row(2), target - col(1))
A[8] += min(target - row(2), target - col(2))
return A
function result: [9, 1, 1, 1, 9, 1, 1, 1, 9]
function result: [10, 1, 1, 1, 9, 2, 1, 2, 9]
[9, 1, 1, 1, 1, 1, 1, 1, 8]
[1, 1, 1, 1, 9, 1, 1, 2, 1]
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
def row(i):
return A[i * 3] + A[i * 3 + 1] + A[i * 3 + 2]
def col(i):
return A[i] + A[i + 3] + A[i + 6]
# target the largest existing sum as the sum for each row
# this should allow for the fewest number of increments
target = max([row(0), row(1), row(2), col(0), col(1), col(2)])
# increment as much as needed based on row/col constraints
A[0] += min(target - row(0), target - col(0))
A[1] += min(target - row(0), target - col(1))
A[2] += min(target - row(0), target - col(2))
A[3] += min(target - row(1), target - col(0))
A[4] += min(target - row(1), target - col(1))
A[5] += min(target - row(1), target - col(2))
A[6] += min(target - row(2), target - col(0))
A[7] += min(target - row(2), target - col(1))
A[8] += min(target - row(2), target - col(2))
return A
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
def row(i):
return A[i * 3] + A[i * 3 + 1] + A[i * 3 + 2]
def col(i):
return A[i] + A[i + 3] + A[i + 6]
# target the largest existing sum as the sum for each row
# this should allow for the fewest number of increments
target = max([row(0), row(1), row(2), col(0), col(1), col(2)])
# increment as much as needed based on row/col constraints
A[0] += min(target - row(0), target - col(0))
A[1] += min(target - row(0), target - col(1))
A[2] += min(target - row(0), target - col(2))
A[3] += min(target - row(1), target - col(0))
A[4] += min(target - row(1), target - col(1))
A[5] += min(target - row(1), target - col(2))
A[6] += min(target - row(2), target - col(0))
A[7] += min(target - row(2), target - col(1))
A[8] += min(target - row(2), target - col(2))
return A
function result: [9, 1, 1, 1, 9, 1, 1, 1, 9]
function result: [10, 1, 1, 1, 9, 2, 1, 2, 9]
function result: [1, 1, 1, 1, 1, 1, 1, 1, 1]
[9, 1, 1, 1, 1, 1, 1, 1, 8]
[1, 1, 1, 1, 9, 1, 1, 2, 1]
[1, 1, 1, 1, 1, 1, 1, 1, 1]
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
def row(i):
return A[i * 3] + A[i * 3 + 1] + A[i * 3 + 2]
def col(i):
return A[i] + A[i + 3] + A[i + 6]
# target the largest existing sum as the sum for each row
# this should allow for the fewest number of increments
target = max([row(0), row(1), row(2), col(0), col(1), col(2)])
# increment as much as needed based on row/col constraints
A[0] += min(target - row(0), target - col(0))
A[1] += min(target - row(0), target - col(1))
A[2] += min(target - row(0), target - col(2))
A[3] += min(target - row(1), target - col(0))
A[4] += min(target - row(1), target - col(1))
A[5] += min(target - row(1), target - col(2))
A[6] += min(target - row(2), target - col(0))
A[7] += min(target - row(2), target - col(1))
A[8] += min(target - row(2), target - col(2))
return A
function result: [9, 1, 1, 1, 9, 1, 1, 1, 9]
function result: [10, 1, 1, 1, 9, 2, 1, 2, 9]
function result: [1, 1, 1, 1, 1, 1, 1, 1, 1]
[9, 1, 1, 1, 1, 1, 1, 1, 8]
[1, 1, 1, 1, 9, 1, 1, 2, 1]
[1, 1, 1, 1, 1, 1, 1, 1, 1]
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
def row(i):
return A[i * 3] + A[i * 3 + 1] + A[i * 3 + 2]
def col(i):
return A[i] + A[i + 3] + A[i + 6]
# target the largest existing sum as the sum for each row
# this should allow for the fewest number of increments
target = max([row(0), row(1), row(2), col(0), col(1), col(2)])
# increment as much as needed based on row/col constraints
A[0] += min(target - row(0), target - col(0))
A[1] += min(target - row(0), target - col(1))
A[2] += min(target - row(0), target - col(2))
A[3] += min(target - row(1), target - col(0))
A[4] += min(target - row(1), target - col(1))
A[5] += min(target - row(1), target - col(2))
A[6] += min(target - row(2), target - col(0))
A[7] += min(target - row(2), target - col(1))
A[8] += min(target - row(2), target - col(2))
return A
The solution obtained perfect score.
Random matrices with medium value range, max - min <= 2000, and two zeros.