in reply to Filling buckets
Indeed try some sample code out like this:sub split_to_n { my $n = shift; my @p = map {(@_)*$_/$n} 0..$n; map {[@_[$p[$_-1]..($p[$_]-1)]]} 1..$n; }
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:use Data::Dumper; $Data::Dumper::Indent = 1; print Dumper split_to_n(3, 'a'..'z');
May I submit that the spec you gave is twisted? :-)sub split_to_n { my $n = shift; my @p = map {(@_)*$_/$n} 0..$n; @_ = reverse @_; map {[reverse @_[$p[$_-1]..($p[$_]-1)]]} reverse 1..$n; }
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 | |
by coreolyn (Parson) on Jan 03, 2001 at 20:22 UTC | |
by tilly (Archbishop) on Jan 03, 2001 at 20:40 UTC | |
by I0 (Priest) on Jan 09, 2001 at 21:17 UTC | |
by coreolyn (Parson) on Jan 09, 2001 at 22:33 UTC |