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.
A reasonable optimizing compiler will produce the same machine code regardless of whether the variable is defined before the if-check or after it. Initializing local variables has no effects visible to callers so the compiler may freely reorder the code as it sees fit.
The reason is just style and clarity. First of all, it's common to add preconditions to the start of a function. If there are no items, we already know the answer and we can return right away. Second, it's generally a good idea to keep the scope of variables as small as possible.
Of course, in such a short function it doesn't really matter, but if a long function defines variable in the very beginning and uses it a lot later, can you be sure that there are no uses between? You have to carefully read through the whole function to check if there are other references.
This comment is hidden because it contains spoiler information about the solution
Quickselect has O(n**2) in the worst case though. Just iterating over the list and setting the two biggest like in my solution would be optimal in runtime
This comment is hidden because it contains spoiler information about the solution
Please, someone explain what means this "x" at the beginning
Top.
Your
filterS
results in StackOverflowError for Streams with rare satisfying elements, e.g. inNice
primeS()
, although somewhat slow. Try, for example, this:This is awesome! My solution is O(n) too but yours is abolutely concise and elegant!
This comment is hidden because it contains spoiler information about the solution
I wanted to give this a point as both a best practice as well as clever, but I guess the two are mutually exclusive.
the \w includes numbers letters and underscores so no it will think numbers are letters too and even abreviate numbers! 999999 abbreviates to 949! it will also mistakenly take words separated with underscore as being one word.
Does anyone knows how to print the string? to get the log in codewars dart console.
I tried
print('mystring');
and it doesn't work.Will it consider numbers?
Yes, that's correct and certainly a bug. In extreme cases it might even turn a terminating computation into non-terminating. Annoying n-plus-a-half loop strikes again. Easy to fix, but turns the code into a mess anyway. :)
Great solution!
But I think, it has one sneaky problem.
Your
takeS
will actually compute one more element than needed. This might be a problem, i.e. if the stream is exausted.Loading more items...