so i am working on a kata :
Digitwise addition is a special kind of addition where instead of adding 1 normally to the number, it adds 1 to every digit of that number. If the digit is a 9, we replace it with a 10 without carrying over to the next digit.
Examples
123 -> 234
Task
Program a function that takes two numbers, n and k, and outputs the number of digits in n after applying Digitwise addition k times. Since the answer can be very large, return the answer modulo 1_000_000_007.
Your solution is expected to be O(klogn).
AND MY SOLUTION IS :
import sys
MOD = 10**9 + 7
sys.set_int_max_str_digits(0)
def d2a(digits):
arr= list(map(int,str(digits)))
return arr
def a2d(arr):
length=len(arr)
digit=""
for i in range(length):
digit+=str(arr[i])
return int(digit)
def add(n):
return n+1
def digitwise_addition(digit, K):
for i in range(K):
#CONVERTING INTO ARRAY
arr=d2a(digit)
#ADDING 1 INTO ALL NUMBERS FROM ARRAY
arr=list(map(add,arr))
#CONVERTING ARRAY INTO DIGIT
digit=a2d(arr)
arr=d2a(digit)
return len(arr) %MOD
problem : it passes all the calculations ,but when K is large ,it asks to exceed limit ,and if exceeded, it says : code timeout ,KINDLY HELP ME OUT
seika is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.