-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathzigzag_array.py
More file actions
29 lines (26 loc) · 807 Bytes
/
Copy pathzigzag_array.py
File metadata and controls
29 lines (26 loc) · 807 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
# Zig-zag array is: 5 10 8 12 6 9 4 -> 5 < 10 > 8 < 12 > 6 > 9 < 4
# <><><><><>(Zigzag pattern) OR ><><><><><(zig zag pattern)
arr = list(map(int,input().rstrip().split()))
i = 0
flag = True
#while loop
while (i < len(arr)-1):
if flag == True:
if arr[i] < arr[i+1]:
# increment i and change flag here because we have used continue ...
i += 1
flag = not flag
continue
else:
arr[i], arr[i+1] = arr[i+1], arr[i]
else:
if arr[i] > arr[i+1]: #comarition
i += 1
flag = not flag
continue
else:
arr[i], arr[i+1] = arr[i+1], arr[i]
# this is excuted when flag = 0,1 and condition if false and else part excuted ...
i += 1
flag = not flag
print(*arr)