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.
Not sure about this solution. While the code looks simpler it is not a best practice. It creates a new object everytime you have a consecutive word. In this case it isn't a big deal, but in general it is better to avoid the creation of objects like this, as they are just temporal, and their lifetime is within a loop. In more complex scenarios this could lead to waste of memory and bad performance. It is not a good practice.
This is a really bad solution, at least regarding performance, and definitely not a "Best Practice". These are the issues:
To fix them:
Please see my answer or others that have use the same approach. But please be careful whenever you use some methods, always think about the complexity and the memory usage of your implementations.
This comment is hidden because it contains spoiler information about the solution
Thanks :) I think it counts as a one-liner solution
Interesting solution, yet it is very expensive in terms of space. You are creating a new list for every range, even if it is a one value range. So in worst case you would have n lists where n is the size of the array.
The bound + 1 / 2 is a nice trick that is useful in many situations where you need the same index for odd and even lengths.
Thank you very much for the explanation :)
I found the solution by observation. But I would like a proper math/logic explanation. Can someone help me please?
This is indeed the best practice if you have a "long" parameter. If on the other hand the type parameter was an "int", then it would be safe to use string.repeat(n) as the answer.
At first I thought of this solution as well. However, take into account that the parameter is long for some reason. In real life applications this would be a code smell. This is not a safe cast as you have no control over the argument at runtime. At some point, a client may pass a long, as the type parameter is long, and then the casting will fail with an overflow of the long value.
This comment is hidden because it contains spoiler information about the solution
This is definitely not a good practice. Whenever you see a literal true or false you should always ask yourself if you can get rid of it by analyzing the boolean expression. Most of the time you only need to return the result of the boolean expression, as in this case.
Having a for loop that stops on a condition not related to the defined variable "i" is not a good practice. Usually for loops are used to traverse exhaustively, for example in an array or collection. If you don't know for sure when a loop should stop and you depend on an external condtion as in this case, it is better to use a while loop.
You can also use IntSummaryStatistics, which is very nice when trying to get multiple data from an IntStream. I learned this from other Katas. Please see my solution :)
While this is a clean solution, it is not a good practice, as it does perform badly: O(n*m), where n and m are the sizes of the arrays. Basically you are going though the second array as many times as items you have in the first array. You can accomplish this going through only once each array. See other solutions :) or mine if you wish.
Loading more items...