-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path029_problem.py
More file actions
81 lines (77 loc) · 3.16 KB
/
029_problem.py
File metadata and controls
81 lines (77 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#Multiply two matrices A and B.
def multiply_matrices(matrix1, matrix2):
if len(matrix1[0]) != len(matrix2):
raise ValueError("Number of columns in the first matrix must be equal to the number of rows in the second matrix")
result = []
for i in range(len(matrix1)):
row = []
for j in range(len(matrix2[0])):
sum_product = 0
for k in range(len(matrix1[0])):
sum_product += matrix1[i][k] * matrix2[k][j]
row.append(sum_product)
result.append(row)
return result
# Get input for the first matrix
rows1 = int(input("Enter the number of rows for the first matrix: "))
cols1 = int(input("Enter the number of columns for the first matrix: "))
matrix1 = []
print("Enter the elements of the first matrix:")
for i in range(rows1):
row = list(map(int, input().split()))
matrix1.append(row)
# Get input for the second matrix
rows2 = int(input("Enter the number of rows for the second matrix: "))
cols2 = int(input("Enter the number of columns for the second matrix: "))
matrix2 = []
print("Enter the elements of the second matrix:")
for i in range(rows2):
row = list(map(int, input().split()))
matrix2.append(row)
# Multiply the two matrices and print the result
try:
result = multiply_matrices(matrix1, matrix2)
print("Result of multiplying the two matrices:")
for row in result:
print(row)
except ValueError as e:
print(e)
#Logic for interview
def multiply_matrices(matrix1, matrix2):
if len(matrix1[0]) != len(matrix2):
raise ValueError("Number of columns in the first matrix must be equal to the number of rows in the second matrix")
result = []
for i in range(len(matrix1)):
row = []
for j in range(len(matrix2[0])):
sum_product = 0
for k in range(len(matrix1[0])):
sum_product += matrix1[i][k] * matrix2[k][j]
row.append(sum_product)
result.append(row)
return result
# Get input for the first matrix
rows1 = int(input("Enter the number of rows for the first matrix: "))
cols1 = int(input("Enter the number of columns for the first matrix: "))
matrix1 = []
print("Enter the elements of the first matrix:")
for i in range(rows1):
row = list(map(int, input().split()))
matrix1.append(row)
# Get input for the second matrix
rows2 = int(input("Enter the number of rows for the second matrix: "))
cols2 = int(input("Enter the number of columns for the second matrix: "))
matrix2 = []
print("Enter the elements of the second matrix:")
for i in range(rows2):
row = list(map(int, input().split()))
matrix2.append(row)
# Multiply the two matrices and print the result
try:
result = multiply_matrices(matrix1, matrix2)
print("Result of multiplying the two matrices:")
for row in result:
print(row)
except ValueError as e:
print(e)
# The time complexity of this algorithm is O(n*m*p) where n is the number of rows in the first matrix, m is the number of columns in the first matrix (which is also the number of rows in the second matrix), and p is the number of columns in the second matrix. The space complexity is O(n*p) for storing the result matrix.