Minimum integer that can be obtained by swapping adjacent digits of different parity | Function to return the minimum number ; Store the elements which are divisible by two in stack1 ; Store the elements which are not divisible by two in stack2 . ; Concatenate the answer with smaller value of the topmost elements of both the stacks and then pop that element ; Concatenate the answer with remaining values of stack1 . ; Concatenate the answer with remaining values of stack2 . ; Driver code ; Function calling
def minimumNo ( n ) : NEW_LINE INDENT ans = 0 NEW_LINE stack1 = [ ] NEW_LINE stack2 = [ ] NEW_LINE while ( n != 0 ) : NEW_LINE INDENT r = n % 10 NEW_LINE if ( r % 2 == 0 ) : NEW_LINE INDENT stack1 . append ( r ) NEW_LINE DEDENT else : NEW_LINE INDENT stack2 . append ( r ) NEW_LINE DEDENT n = n // 10 NEW_LINE DEDENT while ( len ( stack1 ) > 0 and len ( stack2 ) > 0 ) : NEW_LINE INDENT if ( stack1 [ - 1 ] < stack2 [ - 1 ] ) : NEW_LINE INDENT ans = ans * 10 + stack1 [ - 1 ] NEW_LINE del stack1 [ - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT ans = ans * 10 + stack2 [ - 1 ] NEW_LINE del stack2 [ - 1 ] NEW_LINE DEDENT DEDENT while ( len ( stack1 ) > 0 ) : NEW_LINE INDENT ans = ans * 10 + stack1 [ - 1 ] NEW_LINE del stack1 [ - 1 ] NEW_LINE DEDENT while ( len ( stack2 ) > 0 ) : NEW_LINE INDENT ans = ans * 10 + stack2 [ - 1 ] NEW_LINE del stack2 [ - 1 ] NEW_LINE DEDENT return ans NEW_LINE DEDENT n1 = 64432 NEW_LINE print ( minimumNo ( n1 ) ) NEW_LINE n2 = 3137 NEW_LINE print ( minimumNo ( n2 ) ) NEW_LINE