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.
- Recursive Digit Sum
- Discussions
Recursive Digit Sum
Recursive Digit Sum
Sort by
recency
|
584 Discussions
|
Please Login in order to post a comment
I really wanted this to work with recursion so I did. After looking here at the comments it seems that multiplying by 'k' after doing the first sum is the best way to optimize away the memory issues. Did this in C++, could be improved upon but i'll settle with this. Also had issues with the way the question was worded, guess I either need to improve my reading comprehension or this needs to be better.
Like others said, converting str to int and doing calculations won't work for the input test cases and Python string conversion defaults.
Doing digit by digit conversions avoids the problem.
My solution:
I first did this by calculating n as an int and then iteratively dividing n by 10 to get the least significant digit and add it to a total, multiplying by k before a recursive call to calculate the result's super digit. But that required setting Python's max string conversion much higher than the default. Leaving the digits as strings until the calculation is made worked out nicer. Here is my finished code with a few notes to myself:
you need to sum first before combine the string
very manual way in python
If someone out there is having any issues with some of the test cases even though their solution is correct you might want to look at the constraints, using a integer then converting that to string to store your sum is not the move, it will work but not for some of the test cases, it might be worth making your sum append to a string or list and see it that pass some of the test cases.