I'm new to programming and studying python. I solved the following "zigzag" question using python language.
You are given an array of integers and your task is to check all the triples of its consecutive elements for being a zigzag.
zigzag- This means for any consecutive triples which holds $ac$ or $a>b<c.$
for example:
- if the array is [2, 3, 1, 4] the output is [1,1] since 2<3>1 and 3>1<4
- if the array is [4, 3, 6, 4, 2] the output is [1, 1, 0] since 4>3<6 and 3<6>4
I have a constraints for the length of the array(<10000) and value of single element(<1000) of the array. Although it's working, I'm not sure whether it is the proper way of coding for these type of questions. I would like to hear your suggestions.
This is my code.
`import sys
import numpy as np
a = np.array([])
b = np.array([])
n = int(input("Enter the length of the array:"))
if 3 < n < 10000:
for j in range(n):
x = int(input("Enter inputs:"))
if x < 1000:
a = np.append(a, [x])
else:
sys.exit("Value is not less than 1000")
print(a)
i = 0
while i < len(a) - 2:
if a[i] < a[i + 1] and a[i + 1] > a[i + 2]:
b = np.append(b, [1])
elif a[i] > a[i + 1] and a[i + 1] < a[i + 2]:
b = np.append(b, [1])
else:
b = np.append(b, [0])
i += 1
print(b)
else:
print("Length is out of bound")`