-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.py
64 lines (45 loc) · 1.74 KB
/
solver.py
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
from algorithms.data import First, Second
# Constants
indentation = " "
# Is multiplication possible?
if First.columns() != Second.rows():
print("Multiplication not possible! Order doesn't match.")
else:
# Result matrix
result_matrix = []
# Loop through every row in first matrix
for first_row_index in range(First.rows()):
# Result row
result_row = []
# Loop through every column for every nth row of first matrix
for second_row_column_index in range(Second.columns()):
# Define current row
first_row = First.block()[first_row_index]
second_column = []
# Loop through each column in second matrix
for second_row_index in range(Second.rows()):
second_column.append(Second.block()[second_row_index][second_row_column_index])
# Total product and sum of whole row-column pair
pair_sum = 0
# Loop through every element to calculate 'pairSum'
for pair_sum_index in range(len(first_row)):
pair_sum += first_row[pair_sum_index] * second_column[pair_sum_index]
# Append 'pairSum' to result row
result_row.append(pair_sum)
# Append 'result_row' to 'result_matrix'
result_matrix.append(result_row)
# Line breaks
print("")
print("")
print("")
# Result Matrix Heading
print("Result:")
# Loop through each row
for result_row_final in result_matrix:
# Default value
final = ""
# Loop through each element
for each_element in result_row_final:
final += str(each_element) + " "
# Print row
print(indentation + final)