Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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 ( [id://892841]=note: print w/replies, xml ) Need Help??


in reply to Re: Split range 0 to M into N non-overlapping (roughly equal) ranges.
in thread Split range 0 to M into N non-overlapping (roughly equal) ranges.

I can make it differently, but no cleaner

It always bugs me when something notionally so simple doesn't seem to have a clean solution. I do hope someone will prove me wrong.

In use, it doesn't matter if the ranges vary by more than the minimum. Up to 1% variability, smallest to largest, would have no appreciable affect. But it still feels like I'm missing some trick that would allow a simple and direct implementation.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
  • Comment on Re^2: Split range 0 to M into N non-overlapping (roughly equal) ranges.

Replies are listed 'Best First'.
Re^3: Split range 0 to M into N non-overlapping (roughly equal) ranges.
by roboticus (Chancellor) on Mar 12, 2011 at 18:02 UTC

    BrowserUk:

    I know what you mean, that's what prompted yesterday's question, in fact. I took a bit of a break, and then I thought to myself "too bad reduce (from List::Util) doesn't have a version that returns an array, and then double-checked myself. It can get pretty close. Here's yet another version, which is far too ugly, but interesting in its own way:

    #!/usr/bin/perl use strict; use warnings; use List::Util qw(reduce); my ($M, $N) = (9999997,5); my $ranges = reduce { ref($a) eq "ARRAY" ? [ @$a, [ $$a[-1][1], $b] ] : [ [ $a, $b ] ] } map { $_*int($M/$N) } 0 .. $N; print join(", ", map { "(" . join("-",@{$_}).")" } @$ranges), "\n"; $ perl 892828_c.pl (0-1999999), (1999999-3999998), (3999998-5999997), (5999997-7999996), +(7999996-9999995)

    I like where it's a single statement, but the special-casing for the first element just sucks. Perhaps I should remove the special case and give it a specially-constructed list:

    #!/usr/bin/perl use strict; use warnings; use List::Util qw(reduce); my ($M, $N) = (9999997,5); my $ranges = reduce { [ @$a, [ $$a[-1][1], $b] ] } [ [ 0, 1 ] ], map { $_*int($M/$N) } 0 .. $N; print join(", ", map { "(" . join("-",@{$_}).")" } @$ranges), "\n"; $ perl 892828_d.pl Name "main::b" used only once: possible typo at 892828_d.pl line 10. (0-1), (1-0), (0-1999999), (1999999-3999998), (3999998-5999997), (5999 +997-7999996), (7999996-9999995)

    Well, it's getting a little better in that it removes the special case code, but it's broken (the first two ranges need to be removed from the result) and it's not very easy to read.

    I think I'll give up on it and move on to my electronics project. Thanks for the interesting diversion!

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      OK, I just couldn't give up on it. I've come up with two final solutions:

      • A short one for "in line" use, and it isn't too ugly, and
      • A subroutine that returns ranges, and also lets you specify the beginning of the range to split.

      $ cat 892828_e.pl #!/usr/bin/perl use strict; use warnings; my ($MIN, $MAX, $N) = (12345, 9999997, 6); ### Inline version: my @R = ( map {$_*int($MAX/$N)} 0 .. $N-1 ); $R[$_] = [$R[$_],$R[$_+1]//$MAX] for 0 .. $N-1; print join("; ", map(join("-",@$_),@R)), "\n\n"; ### Subroutine version: @R = make_ranges($MIN, $MAX, $N); print join("; ", map(join("-",@$_),@R)), "\n\n"; sub make_ranges { my ($min, $max, $N) = @_; return [$min, $max] if $N ==1; my $newmax = $min + int( ($max-$min)/$N ); return [$min, $newmax], make_ranges($newmax+1, $max, $N-1); } Roboticus@Roboticus-PC ~ $ perl 892828_e.pl 0-1666666; 1666666-3333332; 3333332-4999998; 4999998-6666664; 6666664- +8333330; 8333330-9999997 12345-1676953; 1676954-3341562; 3341563-5006171; 5006172-6670780; 6670 +781-8335389; 8335390-9999997

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://892841]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-19 16:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found