human
Write me a code to biggest integer which has maximum digit sum in range from 1 to n and the other steps required given the following:
Biggest integer which has maximum digit sum in range from 1 to n | function to calculate the sum of digits of a number ..
Returns the maximum number with maximum sum of digits ..
initializing b as 1 and initial max sum to be of n.
iterates from right to left in a digit.
while iterating this is the number from right to left.
calls the function to check if sum of cur is more then of ans.
reduces the number to one unit less.
driver program to test the above function"
How can the above be solved in Python?
assistant
#Here is code to biggest integer which has maximum digit sum in range from 1 to n and the other steps required.
 def sumOfDigits ( a ) :
	sm = 0
	while ( a != 0 ) :
		sm = sm + a % 10
		a = a // 10
	return sm
def findMax ( x ) :
	b = 1
	ans = x
	while ( x != 0 ) :
		cur = ( x - 1 ) * b + ( b - 1 )
		if ( sumOfDigits ( cur ) > sumOfDigits ( ans ) or ( sumOfDigits ( cur ) == sumOfDigits ( ans ) and cur > ans ) ) :
			ans = cur
		x = x // 10
		b = b * 10
	return ans
n = 521
print ( findMax ( n ) )