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.
Actually readable code? Shocking.
Incredibly clean. Only way this could be cleaner is if it checked the provided values for being numbers, but that also feels anti-pythonic so I'm going to hazard that this is a perfect answer.
Since nobody has pointed it out yet: this is O(n^2), which is not great. Every time it runs replace it has to read in the entire string, and it has to do this an amount of times that is in worst-case proportionate to the entire string. For example, if my string is ['NORTH', 'WEST', 'NORTH', 'SOUTH', 'EAST', 'SOUTH'] or a similar construction, I can only remove one pair at a time. So for a string with N pairs, I have to read N times, and the lengths each time are (N), (N-1), (N-2), etc.
It is however clever as hell. Behold the power of regex.
Javascript doesn't throw array index out of bounds exceptions. It simply returns undefined.
Since undefined is not equal to any possible value of opposite[dir], this will always evaluate false on the first run of the loop.
This is incorrect. Javascript is pass by value, but the 'value' of an array or other object is itself a reference, so if you mutate it inside of a function it is mutated outside of the function also. To see this, run:
arr = [1, 2, 3];
function change(arr) {
arr.push(4);
}
change(arr);
console.log(arr);
However, this answer does not mutate the passed array 'plan' at all. It is reading in a single value of 'plan' at a time in the reducer, and populating a separate array, 'dirs'.
Stackoverflow post referenced states that switch-immediate (which this is) is actually the fastest.
Also it's eight years old now and js uses a totally different optimizing compiler in 2020.
The filter with side effects alone is enough reason to flunk a code review. I'm iffy on if it's readable or not.
It's really cool. I play code golf too! It's fun and I respect code golf as a skill but oh my god should this not be listed as 'best practices'.
Clever code golf, awful practice
This is undeniably the best solution and belongs at the top.
I didn't get it my first go, did a for loop that scales O(n) like I think most people did, but this is both the cleverest solution and the one that is best practice since it's O(1).
Can confirm that this is a problem in Python. Test suite appears to be running off of an ordered list of all Unicode codes which can plausibly be considered 'letters', and in lieu of being able to import and use whatever library was used to do that or constructing such a list myself the kata can't be solved.
I'm weirdly impressed that whoever set up the test suite did that instead of just doing ASCII. I suspect malicious compliance.
Yeah, python re right now apparently matches the newline with $ and doesn't reject. I had to add to this solution a special condition to return False if there was a newline in the pin, kind of an ugly hack.
Don't know why this one isn't on top for best practices; best runtime of every solution listed, better than a loop and much much better than recursion.