Yes - recursions are fascinating, however, be careful with them. Recursions could be double-edged swords...
Consider that each recursion level costs in memory on the stack. So if you have too many levels (self invocation) you would end up with a crash on lack of memory (or slowing down your whole computer as your code exhausts whole of the available memory).
In this specific case it is probably fine - we assume a number with a reasonable number of digits.
First and foremost "return n if n < 10" is the base case.
The map function applies a given function (in this case "int") to every item of something that is iterable. -> new for me!
Using the string function on the input n, you turn n into a string which is iterable. Therefore the map functiion can now apply a given function to each character of a string. -> the clever part!
Once the map function is done applying int to every digit, it is then all summed together.
easy
Yes - recursions are fascinating, however, be careful with them. Recursions could be double-edged swords...
Consider that each recursion level costs in memory on the stack. So if you have too many levels (self invocation) you would end up with a crash on lack of memory (or slowing down your whole computer as your code exhausts whole of the available memory).
In this specific case it is probably fine - we assume a number with a reasonable number of digits.
WoW
finally I saw a solution similar to mine.
Oh neat!
We learned new stuff.
First and foremost "return n if n < 10" is the base case.
The map function applies a given function (in this case "int") to every item of something that is iterable. -> new for me!
Using the string function on the input n, you turn n into a string which is iterable. Therefore the map functiion can now apply a given function to each character of a string. -> the clever part!
Once the map function is done applying int to every digit, it is then all summed together.
WHAT
it seems he is working for Google and have no time to answer
very cool my boy
This comment is hidden because it contains spoiler information about the solution
I mainly meant efficient in the code golf sense of not requiring many characters. :) Yes, the floor division method is more performant.
This comment is hidden because it contains spoiler information about the solution
This comment is hidden because it contains spoiler information about the solution
This comment is hidden because it contains spoiler information about the solution
yessir
Efficient way to get the digits. I'll have to remember that one.
Loading more items...