human
What is a code to number of local extrema in an array and the other steps required given the following:
Number of local extrema in an array | function to find local extremum.
start loop from position 1 till n - 1.
check if a [ i ] if greater than both its neighbours , then add 1 to x.
check if a [ i ] if less than both its neighbours , then add 1 to x.
driver program"
How can the above be solved in Python?
assistant
#Here is code to number of local extrema in an array and the other steps required.
 def extrema ( a , n ) :
	count = 0
	for i in range ( 1 , n - 1 ) :
		count += ( a [ i ] > a [ i - 1 ] and a [ i ] > a [ i + 1 ] ) ;
		count += ( a [ i ] < a [ i - 1 ] and a [ i ] < a [ i + 1 ] ) ;
	return count
a = [ 1 , 0 , 2 , 1 ]
n = len ( a )
print ( extrema ( a , n ) )