The problem
For any given array, find a corresponding zig-zag array such the each element (a,b,c,d,e...) follows the rule:
a < b > c < d > e < f
In other words, every other element is greater than both of its neighbouring elements and the remaining elements are smaller than their neighbours.
For example, for the list:
[4, 3, 7, 8, 6, 2, 1]
the output is:
[1 8 2 7 3 6 4]
My solution
array = [4, 3, 7, 8, 6, 2, 1]
array.sort()
while len(array) > 1:
print(array[0], array[-1], sep = " ", end= " ")
array.pop(0)
if array: array.pop(-1)
print(array[0])
zip) \$\endgroup\$