Convert Array into Zig-Zag Fashion in Python Using the Function
ADVERTISEMENTS
Convert array into zig-zag fashion in python using the function. There are you will learn how to print the zig-zag fashion of an array.
Take an example to print zig-zag fashion through a python program:
# Convert Array into Zig-Zag Fashion in Python using the Function
def makeZigZag(arru, n):
pass
flag, temp = True, None
for i in range (n - 1):
if flag:
if arru[i] > arru[i + 1]:
temp = arru[i]
arru[i] = arru[i + 1]
arru[i + 1] = temp
else:
if arru[i] < arru[i + 1]:
temp = arru[i]
arru[i] = arru[i + 1]
arru[i + 1] = temp
flag = False
arru, x = [], 0
# arru - it will store array elements
# x - size of array
print ("-----enter the size of the array-----")
x = int (input ())
print ("-----Enter the ", x, " elements one by one-----")
for i in range (x):
arru.append(int (input ()))
# This will change the array position into zig-zag
makeZigZag(arru, x)
print ("\n-----After conversion into zig-zag fashion-----")
for i in range (x):
print (arru[i], end=", ")
print (end="\n")
Output
-----enter the size of the array-----
8
-----Enter the 8 elements one by one-----
12
34
65
34
67
34
67
34
-----After conversion into zig-zag fashion-----
12, 65, 34, 67, 34, 67, 34, 34,