human
Next higher number using atmost one swap operation | function to find the next higher number using atmost one swap operation.
to store the index of the largest digit encountered so far from the right.
to store the index of rightmost digit which has a digit greater to it on its right side.
finding the ' index ' of rightmost digit which has a digit greater to it on its right side.
required digit found , store its ' index ' and break.
if no such digit is found which has a larger digit on its right side.
to store the index of the smallest digit greater than the digit at ' index ' and right to it.
finding the index of the smallest digit greater than the digit at ' index ' and right to it.
swapping the digits.
required number.
Driver program to test above"
How can the above be solved in Python?
 ... Can you provide a code to next higher number using atmost one swap operation and the other steps required?
assistant
#Here is code to next higher number using atmost one swap operation and the other steps required.
 def nextHighUsingAtMostOneSwap ( st ) :
	num = list ( st )
	l = len ( num )
	posRMax = l - 1
	index = - 1
	i = l - 2
	while i >= 0 :
		if ( num [ i ] >= num [ posRMax ] ) :
			posRMax = i
		else :
			index = i
			break
		i = i - 1
	if ( index == - 1 ) :
		return " Not ▁ Possible "
	greatSmallDgt = - 1
	i = l - 1
	while i > index :
		if ( num [ i ] > num [ index ] ) :
			if ( greatSmallDgt == - 1 ) :
				greatSmallDgt = i
			elif ( num [ i ] <= num [ greatSmallDgt ] ) :
				greatSmallDgt = i
		i = i - 1
	temp = num [ index ]
	num [ index ] = num [ greatSmallDgt ] ;
	num [ greatSmallDgt ] = temp ;
	return ' ' . join ( num )
num = "218765"
print " Original ▁ number : ▁ " , num
print " Next ▁ higher ▁ number : ▁ " , nextHighUsingAtMostOneSwap ( num )