human
Longest alternating subsequence | Function for finding longest alternating subsequence.
" inc " and " dec " initialized as 1 as single element is still LAS.
Iterate from second element.
" inc " changes iff " dec " changes.
" dec " changes iff " inc " changes.
Return the maximum length.
Driver Code.
Function Call"
How can the above be solved in Python?
assistant
#Here is code to longest alternating subsequence and the other steps required.
 def LAS ( arr , n ) :
	inc = 1
	dec = 1
	for i in range ( 1 , n ) :
		if ( arr [ i ] > arr [ i - 1 ] ) :
			inc = dec + 1
		elif ( arr [ i ] < arr [ i - 1 ] ) :
			dec = inc + 1
	return max ( inc , dec )
if __name__ == " _ _ main _ _ " :
	arr = [ 10 , 22 , 9 , 33 , 49 , 50 , 31 , 60 ]
	n = len ( arr )
	print ( LAS ( arr , n ) )