Answer to Question #274747 in Python for Roger

Question #274747

Write a function solution that given a three digit integer N and Integer K, returns the maximum possible three digit value that can be obtained by performing at most K increases by 1 of any digit in N


given N=192, N=4, output should be 591

1
Expert's answer
2021-12-03T02:05:57-0500
def maxNumber(n:int, k:int) ->int:
	i = 0
	while k > 0 and i < 3:
		n_str = str(n)
		tmp = int(n_str[i])
		if tmp == 9:
			i += 1
		else:
			new_n_str = n_str[:i] + str(tmp+1) + n_str[i+1:]
			if int(new_n_str) > n:
				n = int(new_n_str)
				k -= 1
	return n


n = 191
k = 4
print(maxNumber(n,k))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS