human
Write me a code to print the two possible permutations from a given sequence and the other steps required given the following:
Print the two possible permutations from a given sequence | Function to check if the sequence is concatenation of two permutations or not.
Computing the sum of all the elements in the array.
Computing the prefix sum for all the elements in the array.
Iterating through the i from lengths 1 to n - 1.
Sum of first i + 1 elements.
Sum of remaining n - i - 1 elements.
Lengths of the 2 permutations.
Checking if the sums satisfy the formula or not.
Function to print the two permutations.
Print the first permutation.
Print the second permutation.
Function to find the two permutations from the given sequence.
If the sequence is not a concatenation of two permutations.
Find the largest element in the array and set the lengths of the permutations accordingly.
Driver code"
How can the above be solved in Python?
assistant
#Here is code to print the two possible permutations from a given sequence and the other steps required.
 def checkPermutation ( arr , n ) :
	sum = 0
	for i in range ( n ) :
		sum += arr [ i ]
	prefix = [ 0 for i in range ( n + 1 ) ]
	prefix [ 0 ] = arr [ 0 ]
	for i in range ( 1 , n ) :
		prefix [ i ] = prefix [ i - 1 ] + arr [ i ]
	for i in range ( n - 1 ) :
		lsum = prefix [ i ]
		rsum = sum - prefix [ i ]
		l_len = i + 1
		r_len = n - i - 1
		if ( ( ( 2 * lsum ) == ( l_len * ( l_len + 1 ) ) ) and ( ( 2 * rsum ) == ( r_len * ( r_len + 1 ) ) ) ) :
			return True
	return False
def printPermutations ( arr , n , l1 , l2 ) :
	for i in range ( l1 ) :
		print ( arr [ i ] , end = " ▁ " )
	print ( " " , end ▁ = ▁ " " ) ;
	for i in range ( l1 , n , 1 ) :
		print ( arr [ i ] , end = " ▁ " )
def findPermutations ( arr , n ) :
	if ( checkPermutation ( arr , n ) == False ) :
		print ( " Not ▁ Possible " )
		return
	l1 = 0
	l2 = 0
	l1 = max ( arr )
	l2 = n - l1
	s1 = set ( )
	s2 = set ( )
	for i in range ( l1 ) :
		s1 . add ( arr [ i ] )
	for i in range ( l1 , n ) :
		s2 . add ( arr [ i ] )
	if ( len ( s1 ) == l1 and len ( s2 ) == l2 ) :
		printPermutations ( arr , n , l1 , l2 )
	else :
		temp = l1
		l1 = l2
		l2 = temp
		printPermutations ( arr , n , l1 , l2 )
if __name__ == ' _ _ main _ _ ' :
	arr = [ 2 , 1 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 1 , 10 , 2 ]
	n = len ( arr )
	findPermutations ( arr , n )