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
defmaxNumber(n:int, k:int) ->int:
i = 0while k > 0and i < 3:
n_str = str(n)
tmp = int(n_str[i])
if tmp == 9:
i += 1else:
new_n_str = n_str[:i] + str(tmp+1) + n_str[i+1:]
ifint(new_n_str) > n:
n = int(new_n_str)
k -= 1return n
n = 191
k = 4print(maxNumber(n,k))
"assignmentexpert.com" is professional group of people in Math subjects! They did assignments in very high level of mathematical modelling in the best quality. Thanks a lot
Comments