Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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

by kcott (Archbishop)
on Oct 13, 2015 at 05:59 UTC ( [id://1144620]=note: print w/replies, xml ) Need Help??


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

G'day fidda,

This does what you want:

#!/usr/bin/env perl use strict; use warnings; use Data::Dump; my @A = qw{H1 H2 H3 H4}; my @B = 1 .. 10; my %result; my $reverse = 0; while (@B) { build_hash(\%result, \@A, \@B, $reverse); $reverse ^= 1; } dd \%result; sub build_hash { my ($result, $A, $B, $reverse) = @_; my @keys = @$B >= @$A ? @$A : @$A[0 .. $#$B]; my @values; if ($reverse) { unshift @values, shift @$B for (0 .. $#keys); } else { push @values, shift @$B for (0 .. $#keys); } push @{$result->{$keys[$_]}}, $values[$_] for 0 .. $#keys; }

Output:

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

Note that this code destroys @B. If you need to keep it, make a copy and use that in the while condition and when calling &build_hash:

my @B_copy = @B; ... while (@B_copy) { build_hash(\%result, \@A, \@B_copy, $reverse); ...

Furthermore, the subroutine &build_hash has no reliance on the initial values of either @A or @B. For example, these initial values:

my @A = qw{H1 H2 H3 H4 H5}; my @B = 1 .. 23;

produce this output:

{ 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], }

— Ken

Replies are listed 'Best First'.
Re^2: How to get this not the usual round robin looping
by Athanasius (Archbishop) on Oct 13, 2015 at 06:15 UTC

    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,

      "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

      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;
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      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.
      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.

      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,

        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://1144620]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-18 04:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found