I am trying to reverse an array.
What I am doing is that I want the largest number, followed by the smallest number, followed by the second largest number, followed by the second smallest number, etc.
I have the code but doesn't work for all cases.
def meanderingArray(arr) :
n = len(arr)
arr.sort()
tempArr = [0] * (n + 1)
ArrIndex = 0
i = 0
j = n-1
while(i <= n // 2 or j > n // 2 ) :
tempArr[ArrIndex] = arr[j]
ArrIndex = ArrIndex + 1
tempArr[ArrIndex] = arr[i]
ArrIndex = ArrIndex + 1
i = i + 1
j = j - 1
for i in range(0, n) :
arr[i] = tempArr[i]
return arr
meanderingArray([22231,44423,66574,-14122,-12322,14476])
IndexError Traceback (most recent call last)
<ipython-input-28-8993ecc448e2> in <module>
----> 1 meanderingArray([22231,44423,66574,-14122,-12323,14476])
<ipython-input-26-24690fd05e2d> in meanderingArray(arr)
14 tempArr[ArrIndex] = arr[j]
15 ArrIndex = ArrIndex + 1
---> 16 tempArr[ArrIndex] = arr[i-1]
17 ArrIndex = ArrIndex + 1