Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

How to shuffle this AoA

by karlgoethebier (Abbot)
on Dec 17, 2020 at 14:30 UTC ( [id://11125339]=perlquestion: print w/replies, xml ) Need Help??

karlgoethebier has asked for the wisdom of the Perl Monks concerning the following question:

Given this data:

@AoA = ( [ 2, 3, 4, 5 ], [ 2, 3, 4, 5 ], [ 2, 3, 4, 5 ], );

What i want is something like this:

@AoA = ( [ 3, 2, 4, 5 ], [ 5, 3, 2, 4 ], [ 2, 5, 4, 3 ], ); # many more

But not:

@AoA = ( [ 3, 2, 4, 5 ], [ 4, 3, 2, 5 ], [ 2, 5, 4, 3 ], );

And not:

@AoA = ( [ 3, 2, 4, 5 ], [ 5, 3, 2, 4 ], [ 3, 2, 4, 5 ], );

I have no plan for the moment. And probably even the title of this question is bad.

Thanks in advance for any advice.

Update: Thanks to all for the kind and inspiring replies. I guess i need to rethink my specs, right?

Some words about the background. In the room where i use to record there is too much reverb. By chance i stumbled over this funny video. A DIY "Acoustic Skyline Diffuser". The carpentry work is fubar but the idea is good and the thing looks good. I would use some precut MDF panel and some precut balsa blocks instead and good is. But i wondered how to find a distribution for the blocks.

Another interesting question is if the proportions of the length of the blocks should be integer or not to reach maximum diffusion. But i'm not an audio engineer. And this problem is far beyond this forum. Another solution would be to buy a pro diffusor and forget about the DIY approach.

Best regards, Karl

«The Crux of the Biscuit is the Apostrophe»

perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Replies are listed 'Best First'.
Re: How to shuffle this AoA
by hippo (Bishop) on Dec 17, 2020 at 14:36 UTC

    I must admit that I am struggling to deduce the criteria for which is an allowed shuffle and which is not. Perhaps you could explain?


    🦛

      My guess is that a row is not allowed if it is a 'shifted' version of any previous row. There must be an existing name for such things.

      UPDATE: The name that I wanted is Cyclic-Permutation (The 'restricted definition').

      Bill

      G'day hippo,

      I'm seeing this pattern:

      • In the first (acceptable) result, all elements have a different order and no elements have been previously used.
      • In the second (unacceptable) result: the first element has the same order; the third element has been previously used.

      I put this here to keep all the guesswork together. It was an afterthought; see code written earlier.

      — Ken

      My guess: No values repeated in a column. But let's wait for a reply.

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
        My guess: No values repeated in a column.

        That was my first guess too, but the 4's are repeated in the third column of the first example.

      I must admit that I am struggling to deduce the criteria for which is an allowed shuffle and which is not.

      I did sit puzzled for a short while wondering if I was missing something obvious.
      It's a relief to learn I am not the only one struggling to comprehend the criteria!

Re: How to shuffle this AoA
by haukex (Archbishop) on Dec 17, 2020 at 15:22 UTC

    Though I very much agree with hippo that the rules are quite unclear, let me take a guess: In the first counterexample, in each of the arrays only one pair of elements has been swapped from the original. In the second example, the first and third row are the same. Are you perhaps looking for something along the lines of derangements from Algorithm::Combinatorics?

    $ perl -MData::Dump -MAlgorithm::Combinatorics=derangements \ -e 'dd derangements([ 2, 3, 4, 5 ])' ( [3, 2, 5, 4], [3, 4, 5, 2], [3, 5, 2, 4], [4, 2, 5, 3], [4, 5, 2, 3], [4, 5, 3, 2], [5, 2, 3, 4], [5, 4, 2, 3], [5, 4, 3, 2], )
Re: How to shuffle this AoA
by kcott (Archbishop) on Dec 18, 2020 at 02:02 UTC

    G'day Karl,

    As we're all making guesses, here's mine:

    use strict; use warnings; use constant { SEP => $;, TRY => 5, # arbitrary }; use Data::Dump; use List::Util "shuffle"; my @AoAs = ( [ [ 2, 3, 4, 5 ], [ 2, 3, 4, 5 ], [ 2, 3, 4, 5 ], ], [ [ 3, 2, 4, 5 ], [ 4, 3, 2, 5 ], [ 2, 5, 4, 3 ], ], ); for (@AoAs) { dd $_; randomise($_); } sub randomise { my ($AoA) = @_; my %seen; @seen{map mk_key(@$_), @$AoA} = (1) x @$AoA; for my $i (0 .. $#$AoA) { my $tries = 0; while (1) { my @shuffled = shuffle @{$AoA->[$i]}; if ($seen{mk_key(@shuffled)}++) { die "Too many tries" if ++$tries >= TRY; } else { $AoA->[$i] = [@shuffled]; last; } } } dd $AoA; } sub mk_key { join SEP, @_; }

    Output from two example runs:

    [[2 .. 5], [2 .. 5], [2 .. 5]] [[2, 4, 3, 5], [2, 3, 5, 4], [2, 5, 3, 4]] [[3, 2, 4, 5], [4, 3, 2, 5], [2, 5, 4, 3]] [[5, 4, 3, 2], [4, 5, 2, 3], [5, 2, 3, 4]]
    [[2 .. 5], [2 .. 5], [2 .. 5]] [[4, 5, 2, 3], [3, 5, 2, 4], [2, 5, 4, 3]] [[3, 2, 4, 5], [4, 3, 2, 5], [2, 5, 4, 3]] [[5, 4, 3, 2], [3, 4, 5, 2], [2, 5, 3, 4]]

    — Ken

Re: How to shuffle this AoA
by LanX (Saint) on Dec 17, 2020 at 15:46 UTC
    I want a pony but neither horse nor zebra.

    Come on....You know what I mean....

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      Yeah, there was one on that TV show that was so popular. The one with the guy who wore that shirt with the thing.

      No, the other thing.

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

        > No, the other thing.

        That t-shirt broke the code of conduct and this guy was banned from all media!

        How dare you mentioning him?

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

      Thanx. I guess I'll send you some Bethmännchen for Christmas. But I'll replace the almonds with chili, the sugar with salt and the rose water with vinegar. Creative "Cross Boarder Cooking". Abwechslung erfreut.

      «The Crux of the Biscuit is the Apostrophe»

      perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

        Yummy! <3

        But I can't accept that offer without giving you something too!

        Enjoy! :)

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

Log In?
Username:
Password:

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

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

    No recent polls found