in reply to Filling buckets

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. :-)

Replies are listed 'Best First'.
Re: Re: Filling buckets
by I0 (Priest) on Jan 03, 2001 at 14:49 UTC
    #another twist sub split_to_n { my $n = shift; my @p = map {(@_)*$_/$n} -$n..0; map {[@_[$p[$_-1]..($p[$_]-1)]]} 1..$n; }

      io,

      While I am greatly enjoying your terse and good solutions (Even attempting to emulate your approach to answering questions to some extent), I find I do have a request. If possible, please use slightly more verbosity in your variables. It improves the readability of the code for us less fluent, and allows less expirienced monks like myself to focus on your construction more closely.

      coreolyn -- Working to have perl code flying off his fingers instead of spelling mistakes.

        In this case the variable names are clearly my fault. My names tend to be more verbose when I am not being silly.

        Sorry.

        Thank you for the feedback, I'll try to keep that your suggestion in mind.
        In this case I was trying to emphasize the similarity to tilly's code.
        But I'm interested to know what variable names you would have chosen for improved readability?