We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Initially I tried expanding the string n by k multiple:
full_integer=n*k
This lead to runtime error in 4 hidden cases.
Instead, summing all digits in n then multiplying by k solves the runtime issue.
defint_to_int_arr(integer):returnlist(map(int,str(integer)))defsum_array(n):iflen(n)!=1:new_int=sum(n)int_arr=int_to_int_arr(new_int)returnsum_array(int_arr)else:# One digit left in integerreturnn[0]defsuperDigit(n,k):# Cast n string to int arrayint_arr=list(map(int,n))# Multiply sum by ksum_multiple=sum(int_arr)*k# Cast sum multiple to int arrayint_arr=int_to_int_arr(sum_multiple)returnsum_array(int_arr)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Recursive Digit Sum
You are viewing a single comment's thread. Return to all comments →
Python 3 solution
Initially I tried expanding the string n by k multiple:
This lead to runtime error in 4 hidden cases.
Instead, summing all digits in n then multiplying by k solves the runtime issue.