Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
fail?
solution1('123')
=> 6
solution2('123')
=> 6
solution3('123')
=> 150
EmBRACE the curly braces...
awesome!
amazing👍
Really...?...here you go ;-)
Clever, I'm not sure how it gets any faster than that.
Code here if you want to try:
His solution is actually much faster than mine for larger data sets and longer numbers. Benchmark testing on Repl.it given the following parameters:
10,000 strings of length
L
where 500 <=L
< 1,000string.to_i.digits.sum -- 21.563347 seconds
string.chars.sum(&:to_i) -- 3.311112 seconds
However, my solution is like 2 or 3 characters shorter so clearly mine is more efficient!
Using Repel.it I tested both solutions on over 100
iterations of the test cases and the results are:
string.to_i.digits.sum: 1.1976970482
string.chars.sum(&:to_i): 1.2447343944
So while your solution is unique, the times for
both solutions are about the same.
Cool
If n was type of number, spread operator would not work with it (would throw error). Spread operators only work with arrays and string.
BTW, your solution was clever and simple
...for some reason, I thought n is of type number