Is there a good method for traversing the elements of a given grid in a inward-bound spiral path? let's say i have a 7x3 grid of characters:
a b c d e f g
h i j k l m n
o p q r s t u
i'm storing the grid in a single array, though a list of lists would work equally well. so my input is ('a'..'u') and i want my output to be 'abcdefgnutsrqpohijklm', where the traversal starts at (0,0) and spirals inward clockwise.
this is supposedly used frequently in simple ciphers techniques, but i can't find any code for it. the only algorithm i can find is for the spiralling integer problem like that in node 487200, though i can't seem to adapt it.
the only method i can think of (that i'm algorithmically capable of it seems) is very ugly and probably very inefficient- remove the first row, then rotate the array counter-clockwise, and repeat.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.