Ad
  • Custom User Avatar

    As a beginner-level kata plus the title having the switch keyword, the intro to switch-case usage is perfectly valid here. Though the usage of a lookup table or dictionary would be the ideal way of solving, users can refer to solution panel to view other solutions. OTOH, there exists other 8kyu katas which introduce techniques mentioned above, including what you've mentioned.

  • Default User Avatar

    A whole lot less code and a whole lot faster than my first solution :)

  • Default User Avatar

    I think the reasoning for the first image is: if cell (1,2) and cell (2,2) are both part of the house, then that's a contiguous space, and the mouse is actually running across an open floor (which it's not allowed to do).

    In the second image, the corners of two cells touch, so you have the same "crossroads" form that you could also get from intersecting line segments. Arriving at the crossroads, the mouse COULD politely turn left/right to define a contiguous area -- OR it could squeeze straight through the infinitely tiny corner, loop around the central square, and then cross its own path again. So that shape isn't permitted to exist in the house either.

  • Custom User Avatar

    It seems you didn't read the kata description:

    A prime number (or a prime) is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers.

  • Custom User Avatar

    Your solution is assuming that prime numbers can't be negative. But that's not your fault since the author is having the same problem.

  • Default User Avatar

    The direction to use switch statements feels a little forced here. Since there's a 1:1 input/output relation, and the input is always an integer (starting from 0, in fact), an array of key/value pairs feels way more intuitive than writing out the full syntax of a switch statement. I think it's good not just to teach newcomers how to use certain approaches, but also how to identify the best one.

    A more switch-suited problem might be one where the function has to behave differently based on the input's type, or based on the outcome when a certain operation is performed on it.

  • Default User Avatar

    In the context of a function that can be expressed as a very short chain of basic methods, I think this syntax is fine and readable.

    Obviously you wouldn't cram an extensive and complex function into this kind of one-line solution, but all THIS function needs to do is apply a few methods to a single argument and return it. Perfect use-case for shorthands if there ever was one. If we're worried about who else has to read and work with the code, giving each method its own line would resolve that well enough.