human
Give me a code to rearrange positive and negative numbers with constant extra space and the other steps required given the following:
Rearrange positive and negative numbers with constant extra space | Python implementation of the above approach.
Loop until arr [ i ] < 0 and still inside the array.
Loop until arr [ j ] > 0 and still inside the array.
if i is less than j.
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to rearrange positive and negative numbers with constant extra space and the other steps required.
 def RearrangePosNeg ( arr , n ) :
	i = 0
	j = n - 1
	while ( True ) :
		while ( arr [ i ] < 0 and i < n ) :
			i += 1
		while ( arr [ j ] > 0 and j >= 0 ) :
			j -= 1
		if ( i < j ) :
			arr [ i ] , arr [ j ] = arr [ j ] , arr [ i ]
		else :
			break
arr = [ - 12 , 11 , - 13 , - 5 , 6 , - 7 , 5 , - 3 , - 6 ]
n = len ( arr )
RearrangePosNeg ( arr , n )
print ( * arr )