Elements of anti diagonal
Write a program to print the anti-diagonal elements in the given matrix.
input
The first line of input will contain an integer N, denoting the number of rows and columns of the input matrix.
the second line of input will contain N space-separated integers denoting the elements of each row of the matrix.
output
The output should be a list containing the anti-diagonal elements.
explanation
For example, if the given N is 3, and the given matrix is
1 2 3
4 5 6
7 8 9
The anti-diagonal elements of the above matrix are 3 5 7. so the output should be the list
[3, 5, 7]
sample input 1
3
1 2 3
4 5 6
7 8 9
sample output 1
[3, 5, 7]
sample input 2
5
44 71 46 2 15
97 21 41 69 18
78 62 77 46 63
16 92 86 21 52
71 90 86 17 96
sample output 2
[15, 69, 77, 92, 71]
def readMatrix():
n = int(input())
A = []
for i in range(n):
line = input()
row = [int(s) for s in line.split()]
A.append(row)
return A
def antiDiagonal(A):
n = len(A)
d = [A[i][-1-i] for i in range(n)]
return d
def main():
A = readMatrix()
d = antiDiagonal(A)
print(d)
if __name__ == '__main__':
main()
Comments
Leave a comment