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.
IF you swap the positions of any of the special characters with each other, your solution won't notice -- among other false positive errors.
Of course a regex is not NECESSARY, but it is a very concise and clear way to solve the problem.
Please use spoiler flag next time.
This comment is hidden because it contains spoiler information about the solution
Its harder to understand your solution. Is it really better than regex?
I think what OP means is reusing input values, not necessarily "mutating" them.
The input values are
Number
primitives. They can't be mutated.What you call WET, someone else might call readable.
Much Better!!!
Your RegExp solution is not more efficient. RegExp, in general, is slower than looping through. Although, for a simple case like this, the difference is minimal. The issue with your solution is that you are also sorting. This makes it much slower.
Also, re-defining the argument is fine, especially for such a simple function and non-referenced string type.
what poor practices are you speaking of? looking for examples to avoid pitfalls in the future
I guess you are referring to
JavaScript
, wherereturn 0;
So that's number, number, number yet you "started assuming it was a string". Sorry, I cannot help you.
The only time where mutating the input causes problems is when you mutate the content of the exact object being passed in. Reassigning will always leave the original thing (primitive/object) intact.
This comment is hidden because it contains spoiler information about the solution
It's not the author who ranks but a moderator.
It doesn't modify the input.
string.toLowerCase
returns a new string, and assigning it tostring
only modifies the local variablestring
. Anyway, Javascript strings are immutable so you can't modify the input.You could say that it's bad to re-use parameters in this way, but that's different from modifying the argument, which implies side effects.
I agree that the performance could be better, although this is actually asymptotically optimal O(n). It is O(26 * n) = O(n).
I second all this
Loading more items...