Do you really care which get 8 and which get 9? The
following almost does it:
sub split_to_n {
my $n = shift;
my @p = map {(@_)*$_/$n} 0..$n;
map {[@_[$p[$_-1]..($p[$_]-1)]]} 1..$n;
}
Indeed try some sample code out like this:
use Data::Dumper;
$Data::Dumper::Indent = 1;
print Dumper split_to_n(3, 'a'..'z');
and it works except that the group of 9 is the last
rather than the first. The following less compact
version, however, satisfies the given spec perfectly:
sub split_to_n {
my $n = shift;
my @p = map {(@_)*$_/$n} 0..$n;
@_ = reverse @_;
map {[reverse @_[$p[$_-1]..($p[$_]-1)]]} reverse 1..$n;
}
May I submit that the spec you gave is twisted? :-)
PS This is the kind of code which could benefit from an
explanatory comment about the interface. :-)
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.