forked from vedant1771/Hactoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZigzagpattern_string.py
More file actions
42 lines (30 loc) · 882 Bytes
/
Zigzagpattern_string.py
File metadata and controls
42 lines (30 loc) · 882 Bytes
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
# Function to print given string in the zigzag form in `k` rows
def printZigZag(s, k):
# base case
if k == 0:
return
# base case
if k == 1:
print(s, end='')
return
# print first row
for i in range(0, len(s), (k - 1) * 2):
print(s[i], end='')
# print middle rows
for j in range(1, k - 1):
down = True
i = j
while i < len(s):
print(s[i], end='')
if down: # going down
i += (k - j - 1) * 2
else: # going up
i += (k - 1) * 2 - (k - j - 1) * 2
down = not down # switch direction
# print last row
for i in range(k - 1, len(s), (k - 1) * 2):
print(s[i], end='')
if __name__ == '__main__':
s = 'THISPROBLEMISAWESOME'
k = 4
printZigZag(s, k)