I have a challenge for you to try. First, please look at this code. unspiral (i, j) { n = Math.max(Math.abs(i), Math.abs(j)) 4 * n * n + (j > i ? -1 : 1) * (2 * n - i - j)} function const return By reading the code, did you understand what the code does? How long did it take you to figure that out? The spiral Here’s a 2D spiral pattern: As you can see in the diagram above, the spiral is centered at (0, 0). It starts at the tile above the center, and then works its way clockwise. The function takes the coordinates and returns the corresponding number. Let’s assume that and can be arbitrarily large. unspiral(i, j) (i, j) i j For example: unspiral(0, 0) === 0 unspiral(-2, -1) === 9 unspiral(-123, 456) === 831165 Here’s the solution I came up with: unspiral (i, j) { n = Math.max(Math.abs(i), Math.abs(j)) 4 * n * n + (j > i ? -1 : 1) * (2 * n - i - j)} function const return But as you can see, the code looks very cryptic and mathematical. It might take several minutes to understand the code. And you need to think deeply in order to change or extend it to your needs. A requirement change may require the spiral to: Be centered at a different coordinate. Start running at a different number. Rotate in an opposite direction (counter-clockwise). Start at a different side. I’ve written several pieces code that looked like this, and very few people could change it. Although it’s a pure, small, testable, dependency-free function that does one thing, but it isn’t so pleasant to read and it’s hard to extend. So I thought, maybe, . my code is not clean enough I hope to learn from other developers, that’s why I came up with this challenge. Your challenge is to rewrite the function, in a way that: unspiral A programmer can easily understand what it does and how it works by reading the function’s code. are allowed. Comments A programmer can easily modify, extend or re-use the function to suit their needs. The algorithm runs in O(1). I would like to see your solutions. Thanks for reading.
Share Your Thoughts