Reputation: 21
My code :
R = int(input("Enter the Size of Square Matrix : "))
matrix = []
print("\nEnter the entries row-wise : ")
for i in range(R):
a = []
for j in range(R):
a.append(int(input()))
matrix.append(a)
print("\nMatrix : \n")
for i in range(R):
for j in range(R):
print(matrix[i][j], end=" ")
print()
print("\n")
print("\nBoundary Matrix\n")
for i in range(R):
for j in range(R):
if (i == 0):
print(matrix[i][j])
elif (i == R - 1):
print(matrix[i][j])
elif (j == 0):
print(matrix[i][j])
elif (j == R - 1):
print(matrix[i][j])
else:
print(" "),
print()
output :
Boundary
Matrix
1
2
3
4
6
7
8
9
I'm not able to print the boundary elements in form of a matrix. the output is in form of a straight line.
Please help me to do so.
Note: I have tried changing the position of print() but that didn't help much.
Upvotes: 0
Views: 749
Reputation: 3639
print("\nBoundary Matrix\n")
for i in range(R):
print(
"{}{}{}".format(
"\t".join(map(str, matrix[i])) if i in (0, R - 1) else matrix[i][0],
"" if i in (0, R - 1) else (" \t" * (R - 1)),
"" if i in (0, R - 1) else matrix[i][R - 1],
)
)
Matrix :
1 2 3 4
5 6 7 8
9 10 11 12
13 1 4 15
Boundary Matrix
1 2 3 4
5 8
9 12
13 1 4 15
Upvotes: 1