[48in24 Exercise] [07-09] Spiral Matrix

This issue is a discussion for contributors to collaborate in getting ready to be featured in 48in24. Please refer to this forum topic for more info.

We will be featuring Spiral Matrix from Jul 09 onwards.

Staff jobs

These are things for Erik/Jeremy to do:

  • ☐ Check/update exercise in Problem Specifications
  • ☐ Create + schedule video

Community jobs

For each track:

  • Implement Spiral Matrix
  • Add approaches (and an approaches introduction!) for each idiomatic or interesting/educational approach.
  • Add video walkthroughs (record yourself solving and digging deeper into the exercise).
  • Highlight up to 16 different featured exercises (coming soon)

Existing Approaches

You can use these as the basis for approaches on your own tracks. Feel free to copy/paste/reuse/rewrite/etc as you see fit! Maybe ask ChatGPT to translate to your programming language.

Track Statuses

You can see an overview of which tracks have implemented the exercise at the #48in24 implementation status page.

I’ve currently gathered the following solutions to feature:

  1. Java: while loop and four nested for-loops
    Use a while loop and four for loops for each side

  2. Java: for loop with four nested for loops
    Use a for loop and four for loops for each side

  3. Nim: single for loop using deltas
    Single loop using deltas for offsets

  4. Python: for loop and zip
    Use a for loop and zip

  5. JavaScript: reversing columns and rows
    Reverse columns and rows

  6. F#: nested list comprehension with recursive cell value helper function
    Use a nested list comprehension with a function to determine cell value

  7. Lua: simplified recursive helper function
    Simplified recursive helper function

  8. Scala: recursion
    Define problem as recursive one, going outside-in

  9. Erlang: recursion and rotate
    Use recursion and rotation

  10. Elixir: recursion and rotate + transpose
    Use recursion and rotation + transposing

  11. Python: use complex numbers
    Use complex numbers

  12. Prolog: constraint-based
    Define the constraints for the spiral and have Prolog find the solution

  13. F#: Joey Tuttle’s algorithm
    Adapt Joey Tuttle’s algorithm

If anyone has more suggestions, do let us know!

I also had one similar to your F# solution, but the function is simpler:

Probably not necessary to include though.

I enjoyed this JS solution: DavidOfEarth's solution for Spiral Matrix in JavaScript on Exercism

1 Like

I’m not sure if you have any solution that walks the spiral using complex values. My iteration 3 walks from 1..n and iteration 4 walks from n..1. I think iteration 4 is simpler. It starts at the center, rotates whenever there is no value set at the adjacent location and then advances forward one step.

1 Like

I’ll port for Pyret and Vim script.

1 Like

Here i’m adapting in F# the last algorithm described here.

In short it is based on the fact that the minus scan of the inverse permutation of the flattened spiral yields a predictable pattern ([1; n; -1; -n] for the inward counter-clockwise spiral). From this pattern you can create the spiral quite fast as shown in last section of the article.

The solution in J looks like this:

spiral_matrix=: (, ]) $ [: >: [: /: }.@:(2&#@:(|.@:>:@i.)) +/\@# <:@+:^:(0&~:) $ (, -)@(1&,)
1 Like

That is a really impressive solution. I’ll have to dig into the article and code to try and understand what’s happening :)

I think it’s good to include it as an evolution of the F# version.

I hadn’t. Including it in the list!