Consider a square matrix A of size Nx N and an integer X which is an element of A. Find the row number R and column number C of X in A, and calculate the sum of Rand even, find the sum of the digits of all even numbers in the matrix, and if the sum is odd, then find the sum of digits of all odd numbers in the matrix.
Read the input from STDIN and print the output to STDOUT. Do not write arbitrary strings while reading the input or while printing, as these contribute to the standard outp
Constraints: 1) 1<N<= 35.
II) 0 <= RC < N.
III) X is always an element of A.
IV) Elements of A are unique.
def fun(a):
r = len(a)
c = len(a[0])
x = r + c
s = 0
for i in range(r):
for j in range(c):
if a[i][j]%2 == x%2:
# if a[i][j] has the same odd parity as x
s += a[i][j]
return s
import random
r = 3
c = 3
a = []
for i in range(r):
a.append([random.randint(0, 9) for _ in range(c)])
print("Matrix a:", a)
for i in range(r):
print(a[i])
print("fun(a) =", fun(a))
Comments
Leave a comment