This is a memo for myself.
▼ Question
--A table of n x n is given. --Calculate the absolute value of the total value from the upper left to the lower right and the difference from the upper right to the lower left.
▼sample input
matrix
11 2 4
4 5 6
10 8 -12
python
arr = [[11,2,4],[4,5,6],[10,8,-12]]
▼sample output
python
15
▼my answer
python
def diagonalDifference(arr):
n = len(arr[0])-1
xrr=[]
yrr=[]
i=0
for ar in arr:
xrr.append(ar[i])
yrr.append(ar[n-i])
i += 1
ans = abs(sum(xrr)-sum(yrr))
return ans
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
n = int(input().strip())
arr = []
for _ in range(n):
arr.append(list(map(int, input().rstrip().split())))
result = diagonalDifference(arr)
fptr.write(str(result) + '\n')
fptr.close()
** ・ List definitions cannot be summarized ** List cannot be defined as if i = x = 0 by default. ☓ xrr = yrr = [] * Refers to the same object. ◯xrr=[] yrr=[]
Recommended Posts