Find smallest number formed by inserting given digit | Function to insert X in N and return the minimum value string ; Variable to store length of string N ; Variable to denote the position where X must be added ; If the given string N represent a negative value ; X must be place at the last index where is greater than N [ i ] ; For positive numbers , X must be placed at the last index where it is smaller than N [ i ] ; Insert X at that position ; Return the string ; Given Input ; Function Call
def MinValue ( N , X ) : NEW_LINE INDENT N = list ( N ) ; NEW_LINE ln = len ( N ) NEW_LINE position = ln + 1 NEW_LINE if ( N [ 0 ] == ' - ' ) : NEW_LINE INDENT for i in range ( ln - 1 , 0 , - 1 ) : NEW_LINE INDENT if ( ( ord ( N [ i ] ) - ord ( '0' ) ) < X ) : NEW_LINE INDENT position = i NEW_LINE DEDENT DEDENT DEDENT else : NEW_LINE INDENT for i in range ( ln - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( ( ord ( N [ i ] ) - ord ( '0' ) ) > X ) : NEW_LINE INDENT position = i NEW_LINE DEDENT DEDENT DEDENT c = chr ( X + ord ( '0' ) ) NEW_LINE str = N . insert ( position , c ) ; NEW_LINE return ' ' . join ( N ) NEW_LINE DEDENT N = "89" NEW_LINE X = 1 NEW_LINE print ( MinValue ( N , X ) ) NEW_LINE