human
Check if an array of pairs can be sorted by swapping pairs with different first elements | Function to check if an array is sorted or not.
Traverse the array arr [ ].
Return true.
Function to check if it is possible to sort the array w . r . t . first element.
Stores the ID of the first element.
Traverse the array arr [ ].
If arr [ i ] [ 1 ] is not equal to that of the group.
If array is sorted.
Driver Code"
How can the above be solved in Python?
 Write me a code to check if an array of pairs can be sorted by swapping pairs with different first elements and the other steps required?
assistant
#Here is code to check if an array of pairs can be sorted by swapping pairs with different first elements and the other steps required.
 def isSorted ( arr , N ) :
	for i in range ( 1 , N ) :
		if ( arr [ i ] [ 0 ] > arr [ i - 1 ] [ 0 ] ) :
			return False
	return True
def isPossibleToSort ( arr , N ) :
	group = arr [ 0 ] [ 1 ]
	for i in range ( 1 , N ) :
		if ( arr [ i ] [ 1 ] != group ) :
			return " Yes "
	if ( isSorted ( arr , N ) ) :
		return " Yes "
	else :
		return " No "
if __name__ == ' _ _ main _ _ ' :
	arr = [ [ 340000 , 2 ] , [ 45000 , 1 ] , [ 30000 , 2 ] , [ 50000 , 4 ] ]
	N = len ( arr )
	print ( isPossibleToSort ( arr , N ) )