Given an array of numbers, (or in this case a pandas series) return the longest wiggle subarray. A wiggle array is one such that the numbers alternate in direction- they go up and down strictly. I.e.
Input: [10, 22, 9, 33, 49, 50, 31, 60, 15]
Output: [49, 50, 31, 60, 15]
The array must be formed in sequence, i.e you cant form the array by skipping values.
My naive brute force approach can be found below- I'm having a tough time getting it to work. Any tips?
def zigzag(arr):
result = pd.Series([])
for i in range(len(arr)-2):
if arr[i:i+3] != sorted(arr[i:i+3]) and arr[i:i+3] != sorted(arr[i:i+3], reverse = True): # no three points increase or decrease
result = result.append(pd.Series(arr[i:i+3], index = list(range(i, i+3))))
result = result.loc[~result.index.duplicated(keep='first')]
else:
result = pd.Series([])
return result