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.
great
woow, thanks alot i didnt even know this.
have replaced the
occurrences = Hash.new(0)
withoccurrences = {}
.It throws an error and i dont understand why, who can explain the reason?euyemura,
combination(k)
returns anEnumerable
, not anArray
of all the combinations. TheEnumerable
is lazy and will only find out the next combination when you call for the next value. When calling anEnumerable
method likereduce
on the return value ofcombination(k)
you canbreak
out of the loop and return early in you want, hence not wasting cycles. In this kata, I would break out early if my current sum matchest
exactly. The solution above does not do this so will always have to sum all combinations so there is a bit of room for performance improvement, but not much.Sorry, late response. You can pass a block into
ls.combination(k)
instead of to the.map
function, which would allow you to exit early. However, the target distance is a limit, not a minimum. Since each combination you try might be higher than your last max and lower than the limit, we must visit every single combination to be sure of our answer.Good thinking though =D
Thank you for explanation :)
Hello! yours is top voted so i wanted to ask you and everyone else, is there a better way to do this than to use .combination(k)? If i'm not not mistaken this method creates every single combination, as opposed to doing something like making every combination in order until it creates an array with a sum that is over the target distance and then stopping. I ended up using combination as well, but isn't the time efficiency of it pretty bad? Your solution is super elegant, just want to understand if there's more efficient ways.
how does it work? Any documentation that support this assumtion... =)
This comment is hidden because it contains spoiler information about the solution
This comment is hidden because it contains spoiler information about the solution
Like my solution :)
This comment is hidden because it contains spoiler information about the solution