Ad
  • Custom User Avatar

    Actually readable code? Shocking.

  • Custom User Avatar

    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.

  • Custom User Avatar

    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.

  • Custom User Avatar

    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.

  • Custom User Avatar

    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'.

  • Custom User Avatar

    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.

  • Custom User Avatar

    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'.

  • Custom User Avatar
  • Custom User Avatar

    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).

  • Custom User Avatar

    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.

  • Custom User Avatar

    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.

  • Custom User Avatar

    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.