human
How to check if an instance of 8 puzzle is solvable ? | A utility function to count inversions in given array ' arr [ ] '.
Value 0 is used for empty space.
This function returns true if given 8 puzzle is solvable ..
Count inversions in given 8 puzzle.
return true if inversion count is even ..
Driver code"
How can the above be solved in Python?
assistant
#Here is code to how to check if an instance of 8 puzzle is solvable .
 def getInvCount ( arr ) :
	inv_count = 0
	for i in range ( 0 , 2 ) :
		for j in range ( i + 1 , 3 ) :
			if ( arr [ j ] [ i ] > 0 and arr [ j ] [ i ] > arr [ i ] [ j ] ) :
				inv_count += 1
	return inv_count
def isSolvable ( puzzle ) :
	invCount = getInvCount ( puzzle )
	return ( invCount % 2 == 0 )
puzzle = [ [ 1 , 8 , 2 ] , [ 0 , 4 , 3 ] , [ 7 , 6 , 5 ] ]
if ( isSolvable ( puzzle ) ) :
	print ( " Solvable " )
else :
	print ( " Not ▁ Solvable " )