Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Nice problem! and some nice solutions ... everyone loves a diversion ...

Here's mine. For variety, and cos I'm looking for a new job (wink) I went for an OO approach. Certainly not the least characters, and I'm not sure it adds much in readability, but something different. Perhaps not even that, as I suspect that it is really a dressed up version of the iterative solution that was given already. Anyhow, here it is. I had fun doing it.

The object itself:

package SpiralSq; use strict; sub new { my ($class, $size, $offset) = @_; $offset = 1 unless defined ($offset); my $self = { 'size'=>$size, 'offset' => $offset, 'nextoffset' => (4*($size - 1) + $offset)}; bless $self, $class; return $self; } sub getInside { my ($self) = @_; $self->{'inside'} = new SpiralSq($self->{'size'} - 2, $self->{'nextoffset'}) unless (defined $self->{'inside'}); return $self->{'inside'}; } sub getRow { my ($self, $r) = @_; my $o = $self->{'offset'}; my $s = $self->{'size'}; my $n = $self->{'nextoffset'}; my @row; if ($r == 0) { @row = ($o .. ($s - 1 + $o)); } elsif ($r == $s - 1) { @row = ((2*($s - 1) + $o) .. (3*($s - 1) + $o)); @row = reverse @row; } elsif ($r < $s) { @row = (($n - $r), $self->getInside->getRow($r - 1), ($s -1 + $r + $o)); } else { die "error!"; } return @row; } 1;

And a test script:

#!/opt/perl/bin/perl -w use strict; use SpiralSq; my $n = 18; my $s = new SpiralSq($n); for (0 .. ($n-1)) { print join " ", $s->getRow($_); print "\n"; }


In reply to Re: Spiraling integers by danmcb
in thread Spiraling integers by Elijah

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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, 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, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (6)
    As of 2019-12-13 04:50 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?