Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Ceiling elements of an array!

by GotToBTru (Prior)
on May 06, 2015 at 15:41 UTC ( [id://1125851]=note: print w/replies, xml ) Need Help??


in reply to Ceiling elements of an array!

I think the problem is confusion over the term 'ceiling'. I believe the desire is to divide n modules into two (nearly) equal groups. If n is odd, obviously, one group will be larger.

use strict; use warnings; use POSIX qw(ceil); my $modules = shift; my $first_group = ceil($modules/2);0 printf "Call %d on port 80, %d on port 8080\n",$first_group,$modules - + $first_group;
$: perl divide.pl 8 Call 4 on port 80, 4 on port 8080 $: perl divide.pl 9 Call 5 on port 80, 4 on port 8080 $:

Vaguely related to Distribute the leftovers.

Dum Spiro Spero

Replies are listed 'Best First'.
Re^2: Ceiling elements of an array!
by marinersk (Priest) on May 06, 2015 at 18:00 UTC

    Ah, I think you're right -- with that light now on, re-reading the OP it makes perfect sense. I guess my intuition circuit is operating in low-power mode today.

    Curious why you'd go through the effort of loading POSIXjust to get at ceiling()when, in this context, it's always going to be non-negative numbers and the effect can simply be calculated?

    Other than, of course, that the OP did specifically request it . . .

    my $eltcnt = @datary; my $dspcnt = int(($eltcnt + 0.5) / 2);

    Which could likely be reduced to a single, harder-to-read line of code.

      (the module “in the middle” will be called twice)

      Assuming the above is what is wanted (always an even number of calls), another approach (including efforts of others):

      c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -e "my @modules = qw(zero one two three four five six seven); ;; while (@modules) { dd \@modules; my $n = $#modules / 2; print qq{@modules[ 0 .. $n ] -- @modules[ -$n-1 .. -1 ] \n\n}; pop @modules; } " ["zero", "one", "two", "three", "four", "five", "six", "seven"] zero one two three -- four five six seven ["zero", "one", "two", "three", "four", "five", "six"] zero one two three -- three four five six ["zero", "one", "two", "three", "four", "five"] zero one two -- three four five ["zero", "one", "two", "three", "four"] zero one two -- two three four ["zero", "one", "two", "three"] zero one -- two three ["zero", "one", "two"] zero one -- one two ["zero", "one"] zero -- one ["zero"] zero -- zero

      Update: Added '--' marker to output to make partitioning clearer.


      Give a man a fish:  <%-(-(-(-<

      Simpler yet!

      use strict; use warnings; my $modules = shift; my $second_group = int($modules/2); printf "Call %d on port 80, %d on port 8080\n", $modules - $second_group, $second_group;

      Yeah, brain circuits were irrevocably committed to using ceil() in there somewhere, to no real point in a simple case like this.

      Dum Spiro Spero

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-03-28 11:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found