human
Write me a code to print all possible ways to split an array into K subsets and the other steps required given the following:
Print all possible ways to split an array into K subsets | Utility function to find all possible ways to split array into K subsets.
If count of elements in K subsets are greater than or equal to N.
If count of subsets formed is equal to K.
Print K subsets by splitting array into K subsets.
Print current subset.
If current element is the last element of the subset.
Otherwise.
If any subset is occupied , then push the element in that first.
Recursively do the same for remaining elements.
Backtrack.
Otherwise , push it in an empty subset and increase the subset count by 1.
Break to avoid the case of going in other empty subsets , if available , and forming the same combination.
Function to to find all possible ways to split array into K subsets.
Stores K subset by splitting array into K subsets.
Size of each subset must be less than the number of elements.
Driver Code.
Given array.
Given K.
Size of the array.
Prints all possible splits into subsets"
How can the above be solved in Python?
assistant
#Here is code to print all possible ways to split an array into K subsets and the other steps required.
 def PartitionSub ( arr , i , N , K , nos , v ) :
	if ( i >= N ) :
		if ( nos == K ) :
			for x in range ( len ( v ) ) :
				print ( " { ▁ " , end = " " )
				for y in range ( len ( v [ x ] ) ) :
					print ( v [ x ] [ y ] , end = " " )
					if ( y == len ( v [ x ] ) - 1 ) :
						print ( " ▁ " , end = " " )
					else :
						print ( " , ▁ " , end = " " )
				if ( x == len ( v ) - 1 ) :
					print ( " } " , end = " " )
				else :
					print ( " } , ▁ " , end = " " )
			print ( " " , ▁ end ▁ = ▁ " " )
		return
	for j in range ( K ) :
		if ( len ( v [ j ] ) > 0 ) :
			v [ j ] . append ( arr [ i ] )
			PartitionSub ( arr , i + 1 , N , K , nos , v )
			v [ j ] . remove ( v [ j ] [ len ( v [ j ] ) - 1 ] )
		else :
			v [ j ] . append ( arr [ i ] )
			PartitionSub ( arr , i + 1 , N , K , nos + 1 , v )
			v [ j ] . remove ( v [ j ] [ len ( v [ j ] ) - 1 ] )
			break
def partKSubsets ( arr , N , K ) :
	v = [ [ ] for i in range ( K ) ]
	if ( K == 0 or K > N ) :
		print ( " Not ▁ Possible " , end = " " )
	else :
		print ( " The ▁ Subset ▁ Combinations ▁ are : ▁ " )
		PartitionSub ( arr , 0 , N , K , 0 , v )
if __name__ == ' _ _ main _ _ ' :
	arr = [ 1 , 2 , 3 , 4 ]
	K = 2
	N = len ( arr )
	partKSubsets ( arr , N , K )