-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter9_Part7_3.py
More file actions
34 lines (28 loc) · 1.2 KB
/
Copy pathChapter9_Part7_3.py
File metadata and controls
34 lines (28 loc) · 1.2 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
# Part 7: Programming (PART 3)
# Initialize an array quantity:
quantity = [[2, 4, 3, 6, 9],
[5, 8, 9, 3, 7],
[1, 4, 3, 2, 10]]
# Additional function to print the array in a matrix format:
def prt(array):
for a in array:
print(a)
print("This is the original quantity matrix:")
prt(quantity)
print()
# Function that (1) takes the matrix (matrix), rows (r), columns(c), and row number (r_num) then,
# (2) reverse that row number in the matrix.
def func(matrix, c, r_num):
for i in range(0, c//2): # For every value in a row, from 0 to the middle *NOTE
# The following reverses the values,
# the first with the last, the second with the pre-last, and so on...
temp = matrix[r_num][i]
matrix[r_num][i] = matrix[r_num][c-1-i]
matrix[r_num][c-1-i] = temp
# *NOTE: when swaping, we are modifying the values from and also from the back,
# so, when we are in the half-way, the task it's already done.
# The operation //2 divides per 2, and truncates the decimal (this is taking the half list)
# Altering the matrix with func
func(quantity, 5, 1)
print("This is the quantity matrix with the row reversed:")
prt(quantity)