Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-19 05:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found