Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^2: How to get this not the usual round robin looping

by Athanasius (Archbishop)
on Oct 13, 2015 at 06:15 UTC ( [id://1144621]=note: print w/replies, xml ) Need Help??


in reply to Re: How to get this not the usual round robin looping
in thread How to get this not the usual round robin looping

Hello kcott,

This is good, but can be considerably simplified:

#! perl use strict; use warnings; use Data::Dump; my @A = qw{H1 H2 H3 H4 H5}; my @B = 1 .. 23; my %h; while (@B) { push @{ $h{$_} }, shift @B // () for @A, reverse @A; } dd \%h;

Output:

16:10 >perl 1404_SoPW.pl { H1 => [1, 10, 11, 20, 21], H2 => [2, 9, 12, 19, 22], H3 => [3, 8, 13, 18, 23], H4 => [4, 7, 14, 17], H5 => [5, 6, 15, 16], } 16:12 >

:-)

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^3: How to get this not the usual round robin looping
by kcott (Archbishop) on Oct 13, 2015 at 06:45 UTC
    "This is good, ..."

    Thanks.

    "... but can be considerably simplified"

    Agreed.

    I normally check an OP's home node before replying. If it's a first post, I add a welcome greeting. In this case, it was a second post. The first post was stated to be homework.

    On that basis, I intentionally went with a longer piece of code than I might do normally. I had meant to add a comment that the code could be simplified: clearly, I forgot to do that.

    Perhaps, fidda will tell us which was the more useful.

    — Ken

Re^3: How to get this not the usual round robin looping [updated]
by choroba (Cardinal) on Oct 13, 2015 at 09:25 UTC
    You can also complicate it to avoid the loop (in fact, disguise it as a repetition) and the // operator:
    push @{ $h{$_} }, shift @B for (@A, reverse @A) x (@B / @A / 2), (@A, reverse @A)[ 0 .. @B % (2 * @A) - +1 ];

    Update: Another idea:

    for my $i (0 .. $#A) { $h{ $A[$i] } = [ @B[ grep { my $m = $_ % (2 * @A); grep $_ == $m, $i, 2 * @A - $i - 1 } 0 .. $#B ] ]; }

    TIMTOWTDI, an iterator:

    my $iter = do { my ($pos, $step) = ('0E0', 1); sub { my $r = $H{ $A[$pos] } ||= []; if ($pos % $#A || $pos =~ /E/) { $pos += $step; } else { $pos .= 'E0'; $step *= -1; } return $r } }; push @{ $iter->() }, $_ for @B;
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re^3: How to get this not the usual round robin looping: Boustrophedon distribution
by Discipulus (Canon) on Oct 13, 2015 at 09:39 UTC
    As already said in chat congratulation Athanasius for your Boustrophedon distribution function.I thought your crucial line was difficult to read and my hubris lead me think i could semplify it by playing with the index of @A..

    ..as always i endend with a hyper complicated and cryptic solution, even if my hubrys is partially satisfied: anyway TIMTOWTDI!!
    #! perl use strict; use warnings; use Data::Dump; my @A = qw ( H1 H2 H3 H4); # original set my @B = qw (1 2 3 4 5 6 7 8 9 10); # as per OP my %h; my $i = 0; while (@B) { push @{$h{$A[$i]}},shift @B; $i == $#A ? $i = -1 : $i == -$#A-1 ? $i = 0 : $i < 0 ? $i-- : $i+ ++; } dd %h;


    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re^3: How to get this not the usual round robin looping bustrophedon semplified
by Discipulus (Canon) on Oct 13, 2015 at 10:26 UTC
    oh yes the reverse if for sure the best trick, but you can eliminate entirely the while loop using map instead.

    #! perl use strict; use warnings; use Data::Dump; my @A = qw ( H1 H2 H3 H4); my @B = qw (1 2 3 4 5 6 7 8 9 10); my %h; map {push @{$h{ $_ }},grep {defined} shift @B} (@A, reverse @A) x int +($#B / $#A) ; dd %h;


    L*
    UPDATE: with help of choroba i realized i had no need of int nor of so much repetitons:
    map {push @{$h{ $_ }}, shift @B} (@A, reverse @A) x ($#B / $#A /2) +; ##No it is still not good..

    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re^3: How to get this not the usual round robin looping
by Anonymous Monk on Oct 13, 2015 at 06:56 UTC

    This is good, but can be considerably simplified:  H1 => [1, 10, 11, 20, 21],

    hmm, OP has  H1-> 1,8,9

    Weird question :)( i need this sequence ... imagine the equation which I won't tell you )

      hmm, OP has H1-> 1,8,9

      My reply was addressed to kcott, not the OP, and so I used kcott’s dataset.

      imagine the equation which I won't tell you

      I thought the same at first, but if you lay out the sequence, the pattern becomes obvious when you read vertically:

      { H1 => [1, 8, 9], H2 => [2, 7, 10], H3 => [3, 6], H4 => [4, 5] }

      Discipulus, in the Chatterbox, identified it as an example of Boustrophedon, which seems like a pretty good description. :-)

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

        Ah, see that is the kind of crucial detail the homework would have specified .... my version came out like :)
        { H1 => [1, 8, 9], H2 => [2, 7, 10], H3 => [3, 6, 11], H4 => [4, 5, 12], H5 => [13, 20, 21], }
      The difference is because the OP only had H1-H4, while the post with the simplified version added an H5, making the loop longer.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-04-24 08:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found