Python Online Compiler
Example: Convert Array into Zig-Zag Fashion in Python using the Function
C
C++
C#
Java
Python
PHP
main.py
STDIN
Run
# 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")
8 12 34 65 34 67 34 67 34
Output
Clear
ADVERTISEMENTS