frenchtoast has asked for the wisdom of the Perl Monks concerning the following question:
a b c d e f g h i j k l m n o p q r s t ui'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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: spiral path traversal for a grid
by mrborisguy (Hermit) on Dec 31, 2005 at 18:55 UTC | |
Here's what I came up with, using an iterator. I like the iterator because once it's written, you can kindof just forget about it. You could even throw the iterator in a different file, or module if need be. It's not the most elegant code, but it works, but there is no error checking. The iterator returns undef when it's done, but will start over if you call it again. And now the iterator.
-Bryan | [reply] [d/l] [select] |
|
Re: spiral path traversal for a grid
by negation (Beadle) on Dec 31, 2005 at 10:52 UTC | |
One possible solution would be to traverse the first row until you need to stop, then change the direction and traverse downwards until you need to stop and then traverse to left and eventually up. At that point you would be one row below the starting point. Then just repeat the process until done. I guess that could be the way how I would implement it if I was trying to solve the problem. I hope that helps some- negation | [reply] |
by serf (Chaplain) on Dec 31, 2005 at 13:54 UTC | |
Try it with: at the top and at the bottom, that way it's easier to see what it's doing because the letters correspond to columns and rows. Also with such a large list you might want to format the output with: instead of:
| [reply] [d/l] [select] |
|
Re: spiral path traversal for a grid
by dragonchild (Archbishop) on Dec 31, 2005 at 21:51 UTC | |
Someone else explain it. My criteria for good software:
| [reply] [d/l] |
|
Re: spiral path traversal for a grid
by jdporter (Paladin) on Dec 31, 2005 at 17:49 UTC | |
The thread at Spiraling integers provides a number of techniques that could probably be used, even though the problem being addressed is slightly differently formulated.
We're building the house of the future together.
| [reply] |
|
Re: spiral path traversal for a grid
by TedPride (Priest) on Dec 31, 2005 at 18:56 UTC | |
EDIT: Whose doesn't work for 4 rows, ikegami? I tested mine with a number of different configurations, including several with 4 rows, and it seemed to work just fine. | [reply] [d/l] |
by ikegami (Patriarch) on Dec 31, 2005 at 21:25 UTC | |
| [reply] |