in reply to Split range 0 to M into N non-overlapping (roughly equal) ranges.
Hmmm ... I can make it differently, but no cleaner, unfortunately:
$ cat 892828_a.pl #!/usr/bin/perl use strict; use warnings; my ($M, $N) = (9999997, 3); my @ranges = map { $_ * int($M/$N) } 0 .. $N-1; @ranges = map { [ $ranges[$_], $ranges[$_+1]//$M ] } 0 .. $N-1; print join(", ", map { "(".join("-",@{$_}).")" } @ranges), "\n"; $ perl 892828_a.pl (0-3333332), (3333332-6666664), (6666664-9999997)
That one gets the second value wrong by one. I tried to find a way to stack the maps together, but couldn't figure out how to do it nicely.
This one gets the values right, but uses an uglier map statement, and still requires the "$ranges[-1][1]=$M" line, which I don't particularly care for:
$ cat 892828_b.pl #!/usr/bin/perl use strict; use warnings; my ($M, $N) = (9999997, 3); my $step=0; my @ranges = map { my ($c,$d)=($step,$_*int($M/$N)); $step=$d; [ $c, $ +d-1 ] } 1 .. $N; $ranges[-1][1]=$M; print join(", ", map { "(".join("-",@{$_}).")" } @ranges), "\n"; $ perl 892828_b.pl (0-3333331), (3333332-6666663), (6666664-9999997)
Perhaps one of these will be close enough to inspire a better solution for you?
...roboticus
When your only tool is a hammer, all problems look like your thumb.
Update: Added code tags to a bit of text so it renders correctly.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Split range 0 to M into N non-overlapping (roughly equal) ranges.
by BrowserUk (Patriarch) on Mar 12, 2011 at 17:25 UTC | |
by roboticus (Chancellor) on Mar 12, 2011 at 18:02 UTC | |
by roboticus (Chancellor) on Mar 12, 2011 at 21:31 UTC |