0% found this document useful (0 votes)
5 views3 pages

Matrices

The document provides code implementations for various matrix operations including matrix multiplication, rotation of a square matrix, diagonal traversal of a matrix, and spiral traversal of a matrix. Each section includes a question, followed by Python code that reads input, processes the matrix, and outputs the result. The operations are designed to handle multiple test cases as specified by user input.

Uploaded by

22eg105a63
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Matrices

The document provides code implementations for various matrix operations including matrix multiplication, rotation of a square matrix, diagonal traversal of a matrix, and spiral traversal of a matrix. Each section includes a question, followed by Python code that reads input, processes the matrix, and outputs the result. The operations are designed to handle multiple test cases as specified by user input.

Uploaded by

22eg105a63
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

si-matrices

1. Product of 2 Matrices

Question:
Given two matrices, compute their product if matrix multiplication is valid (number of columns in the first
matrix equals the number of rows in the second matrix). Otherwise, return an error.

Code:

t = int(input()) # Number of test cases


for _ in range(t):
# Read dimensions of the first matrix
r1, c1 = map(int, input().split())
mat1 = []
for i in range(r1):
mat1.append(list(map(int, input().split())))

# Read dimensions of the second matrix


r2, c2 = map(int, input().split())
mat2 = []
for i in range(r2):
mat2.append(list(map(int, input().split())))

# Initialize the result matrix with zeros


res = [[0 for _ in range(c2)] for _ in range(r1)]

# Matrix multiplication
for i in range(r1):
for j in range(c2):
for k in range(c1): # or k in range(r2), since M1 == N2
res[i][j] += mat1[i][k] * mat2[k][j]

# Print the result matrix


for row in res:
print(" ".join(map(str, row)))

2. Rotation of Matrix

Question:
Rotate a square matrix 90 degrees clockwise.

Code:

def rot(mat, n):


for i in range(n):
for j in range(i+1, n):
mat[i][j], mat[j][i] = mat[j][i], mat[i][j]
for i in range(n):
mat[i].reverse()

t = int(input())
for c in range(1, t+1):
n = int(input())
mat = []
for i in range(n):
mat.append(list(map(int, input().split())))
rot(mat, n)
print(f"Test Case #{c}:")
for r in mat:
print(*r)
3. Diagonal Traversal of Matrix

Question:
Given a square matrix, compute the sums of all diagonals (both positive and negative slopes) and print them
in reverse order.

Code:

def diag(mat, n):


diag_sums = []
for d in range(-(n-1), n):
diag_sum = 0
for i in range(n):
j = i + d
if 0 <= j < n:
diag_sum += mat[i][j]
diag_sums.append(diag_sum)
diag_sums.reverse()
return diag_sums

t = int(input())
for _ in range(1, t+1):
n = int(input())
mat = []
for i in range(n):
mat.append(list(map(int, input().split())))
res = diag(mat, n)
print(*res)

4. Spiral Traversal of Matrix

Question:
Given a square matrix, print its elements in spiral order starting from the top-left corner.

Code:

def spiral(mat, n):


top, bottom, left, right = 0, n-1, 0, n-1
res = []
while top <= bottom and left <= right:
for i in range(left, right+1):
res.append(mat[top][i])
top += 1
for i in range(top, bottom+1):
res.append(mat[i][right])
right -= 1
if top <= bottom:
for i in range(right, left-1, -1):
res.append(mat[bottom][i])
bottom -= 1
if left <= right:
for i in range(bottom, top-1, -1):
res.append(mat[i][left])
left += 1
return res

t = int(input())
for _ in range(t):
n = int(input())
mat = []
for i in range(n):
mat.append(list(map(int, input().split())))
result = spiral(mat, n)
print(*result)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy