Matrices and Guns — Lots of Guns

Maya Alexandera
3 min readJan 12, 2021

Not until recently have I watched the movie, ‘The Matrix’. If it weren’t for the ‘John Wick’ series I may have never bothered. I’ve seen all three films (almost back-to-back) and was impressed by the evolution of both the stunts and choreography — becoming more advanced and more complex in each series release.

I knew then that I needed more, if not all, Keanu Reeves action flicks. This moment was one of destiny — it was my time to watch, “The Matrix”. Overall, I enjoyed the plot and the storyline, the costumes were great — would recommend. Moving on.

Baba Yaga aside, inspired by my recent cinematic binge, I will share my solution to a matrix-based code challenge.

(* Editor’s Note: The rest of this post will be about coding, not John Wick, or The Matrix, or Keanu Reeves. However, feel free to leave comments regarding these topics as I am now a huge fan.)

Directions
Write a function that accepts an integer N and returns a NxN spiral matrix.
Examplesmatrix(2)
[[1, 2],
[4, 3]]
matrix(3)
[[1, 2, 3],
[8, 9, 4],
[7, 6, 5]]
matrix(4)
[[1, 2, 3, 4],
[12, 13, 14, 5],
[11, 16, 15, 6],
[10, 9, 8, 7]]

First, what is a matrix?

A matrix is a grid used to store or display data in a structured format. It is often used synonymously with a table, which contains horizontal rows and vertical columns.

Good. So, as the problem states — the return value of this function will be a 2D array where each subarray’s length is equal to the parent-array length, resulting in a ‘square’ matrix.

Next, what is a ‘spiral’ in the context of a matrix?

In the examples above it is clear that the ‘spiral’ begins at index [0][0]. Each ‘leg’ of the spiral will stretch from the lowest available index to the highest available index. The directional pattern is:

  • Top Left to Top Right
  • Top Right to Bottom Right
  • Bottom Right to Bottom Left
  • Bottom Left to Top Left.

Step 1: Create an empty 2D array

[startRow, startCol] are both set to 0, the starting index of the spiral.
[endRow, endCol] are both set to n-1, the last index of the spiral.
counter is set to 1 - the value of the starting index.

How I think of it:

It’s about the relationship between the direction and the orientation.

There are four directions: Left to Right, Right to Left, Top to Bottom, and Bottom to Top. The direction of the spiral will determine the opposite axis since the spiral traverses the matrix in one direction at a time.

So, if it is traversing left to right(start to end) directionally, then the column index will remain the same — resulting in startCol to endCol

(Direction/Orientation)

Left to Right/Top

  • startCol to endCol

Top to Bottom/Right

  • startRow to endRow

Right to Left/Bottom

  • endCol to startCol

Bottom to Top/Left

  • endRow to startRow

Step 2: Translate directional pattern to variables

Top Left to Top Right -> startCol to endCol
Top Right to Bottom Right -> startRow to endRow
Bottom Right to Bottom Left -> endCol to startCol
Bottom Left to Top Left -> endRow to startRow

Step 3: Translate to function

The comments in this snippet describe the logic.

The while loop will run until startCol === endCol and startRow === endRow (eg. There are no more remaining empty cells).

What a wild ride.

Leave a comment if this makes sense, doesn’t make sense, or you are in need of a space to express your adoration for all things Keanu Reeves.

--

--