For this program, the output should not have any brackets
M, N = input().split()
M, N = int(M), int(N)
matrix = []
for _ in range(M):
row = [int(x) for x in input().split()]
matrix.append(row)
K = int(input())
values = []
k = 0
l = 0
while (((N - k) > 0) and ((M - l) > 0)) and ((N - k)*(M-l)) > K:
for i in range(k,N):
values.append(matrix[k][i])
for j in range(l+1,M):
values.append(matrix[j][N-1])
for i in range(N-2,k-1,-1):
values.append(matrix[M-1][i])
for j in range(M-2,l,-1):
values.append(matrix[j][k])
values = values[-K:] + values[:-K]
c = 0
for i in range(k,N):
matrix[k][i] = values[c]
c += 1
for j in range(l+1,M):
matrix[j][N-1] = values[c]
c += 1
for i in range(N-2,k-1,-1):
matrix[M-1][i] = values[c]
c += 1
for j in range(M-2,l,-1):
matrix[j][k] = values[c]
c += 1
values = []
k += 1
l += 1
M -= 1
N -= 1
for i in matrix:
print(i)
SOLUTION FOR THE ABOVE QUESTION
SOLUTION CODE
M, N = input("Enter the number of rows and columns of your matrix separated by space:").split()
M, N = int(M), int(N)
matrix = []
rows_count = 1;
for _ in range(M):
print("Enter the values("+str(N)+") values for row "+str(rows_count)+": ")
rows_count = rows_count+1;
#split the values of the row matrix
row = [int(x) for x in input().split()]
#add the row to your matrix
matrix.append(row)
K = int(input("Enter value of k:"))
values = []
k = 0
l = 0
while (((N - k) > 0) and ((M - l) > 0)) and ((N - k)*(M-l)) > K:
for i in range(k,N):
values.append(matrix[k][i])
for j in range(l+1,M):
values.append(matrix[j][N-1])
for i in range(N-2,k-1,-1):
values.append(matrix[M-1][i])
for j in range(M-2,l,-1):
values.append(matrix[j][k])
values = values[-K:] + values[:-K]
c = 0
for i in range(k,N):
matrix[k][i] = values[c]
c += 1
for j in range(l+1,M):
matrix[j][N-1] = values[c]
c += 1
for i in range(N-2,k-1,-1):
matrix[M-1][i] = values[c]
c += 1
for j in range(M-2,l,-1):
matrix[j][k] = values[c]
c += 1
values = []
k += 1
l += 1
M -= 1
N -= 1
#Now let us print our matrix without having the brackets
#make the output not have brackes
print("Matrix output without brackets")
for i in matrix:
for j in range(0,N+1):
if j == N:
print(i[j])
else:
print(i[j], end=" ")
SAMPLE PROGRAM OUTPUT
Comments
Leave a comment