Skip to content

Instantly share code, notes, and snippets.

@jfarmer
Last active December 24, 2015 00:49
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save jfarmer/b043eee597d2f4934fb2 to your computer and use it in GitHub Desktop.
Save jfarmer/b043eee597d2f4934fb2 to your computer and use it in GitHub Desktop.

Spiral Path

Write a function called "spiral" that takes an NxM array as input and returns a "spiral" path through the array, starting in the upper-left corner and moving clockwise. Use the language of your choice.

For example (in Ruby), if

array = [[1,2,3],
         [8,9,4],
         [7,6,5]]

then

spiral(array) # => [1,2,3,4,5,6,7,8,9]

It should work on any NxM array, so don't assume the input is a square array. However, every row will have the same number of elements.

Your Solution

So there aren't any spoilers, if you decide to solve it please fork this gist and link to the fork in a comment. :)

@jfarmer
Copy link
Author

jfarmer commented Sep 26, 2013

Spoiler Alert!

I can't fork my own gist, so here is my solution: https://gist.github.com/jfarmer/d0f37717f6e7f6cebf72

@mjhea0
Copy link

mjhea0 commented Sep 27, 2013

@TalkativeTree
Copy link

@mjhea0
Copy link

mjhea0 commented Sep 27, 2013

@talkative - similar to your's - https://gist.github.com/mjhea0/2f38deba4ba36870a502

i'd like to move it back into a loop. perhaps tomorrow.

@O-I
Copy link

O-I commented Sep 29, 2013

Similar to yours, but not as elegant: https://gist.github.com/O-I/56d3049d9d99de965383

@eriols
Copy link

eriols commented Sep 29, 2013

@adeogun
Copy link

adeogun commented Oct 1, 2013

@abigezunt
Copy link

Second week of coding school at General Assembly with @tibbon, working with Ruby: https://gist.github.com/abigezunt/9a39f5196e1c5ce4b85a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment