forked from Upendradwivedi/python-programs
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreverseWavePrint.py
More file actions
50 lines (40 loc) · 1.22 KB
/
reverseWavePrint.py
File metadata and controls
50 lines (40 loc) · 1.22 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
# Python3 implementation to print
# reverse wave form of matrix
R = 4
C = 4
# function to print reverse wave
# form for a given matrix
def wavePrint(m, n, arr):
j = n - 1
wave = 1
# m - Ending row index
# n - Ending column index
# i, j - Iterator
# wave - for Direction
# wave = 1 - Wave direction down
# wave = 0 - Wave direction up
while j >= 0:
# Check whether to go in
# upward or downward
if wave == 1:
# Print the element of the
# matrix downward since the
# value of wave = 1
for i in range(m):
print(arr[i][j], end = " "),
wave = 0
j -= 1
else:
# Print the elements of the
# matrix upward since the
# value of wave = 0
for i in range(m - 1, -1, -1):
print(arr[i][j], end = " "),
wave = 1
j -= 1
# Driver code
arr = [ [ 1, 2, 3, 4 ],
[ 5, 6, 7, 8 ],
[ 9, 10, 11, 12 ],
[ 13, 14, 15, 16 ] ]
wavePrint(R, C, arr)