Reduce a number N by at most D to maximize count of trailing nines | Python3 program for the above approach ; Function to find a number with maximum count of trailing nine ; Stores count of digits in n ; Stores power of 10 ; If last i digits greater than or equal to d ; Update res ; Update p10 ; Driver Code ; Function Call
from math import log10 NEW_LINE def maxNumTrailNine ( n , d ) : NEW_LINE INDENT res = n NEW_LINE cntDigits = int ( log10 ( n ) + 1 ) NEW_LINE p10 = 10 NEW_LINE for i in range ( 1 , cntDigits + 1 ) : NEW_LINE INDENT if ( n % p10 >= d ) : NEW_LINE INDENT break NEW_LINE DEDENT else : NEW_LINE INDENT res = n - n % p10 - 1 NEW_LINE DEDENT p10 = p10 * 10 NEW_LINE DEDENT print ( res ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n , d = 1025 , 6 NEW_LINE maxNumTrailNine ( n , d ) NEW_LINE DEDENT