ocehb: (Default)
[personal profile] ocehb
рандомная равномерная сортировка без повторений



use List::Util qw( shuffle );

print @ARGV;
my $a = shift;

my ( @main,@rest );

foreach ( shuffle @ARGV ) {
    if ( $a eq $_ ) { push @rest,$a } else { push @main,$a }
    $a = $_;
}

push @rest,$a; @rest = shuffle @rest;

foreach (0..9) {

    last unless ( @rest );

    my @ary;
    while (($a,$b) = splice @main,0,2) {

        if ( defined $b ) {
            push @ary,$a eq $rest[0] || $b eq $rest[0] ? ( $a,$b ) : ( $a,shift ( @rest ),$b );
        }
        else {
            push @ary,$a eq $rest[0] ? ( $a ) : ( $a,shift ( @rest ) );
        }

    }

    @main = @ary;

}

print @main;



# perl -l sh.pl a a a a a b b b c c c c d e e e e e e e e e e
aaaaabbbccccdeeeeeeeeee
aececeaebeabedaececebea


возможно надо вставить рандомизацию вставки @rest в @main.

Profile

ocehb: (Default)
ocehb

January 2021

S M T W T F S
     12
345 6789
10111213141516
17181920212223
24252627282930
31      

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Feb. 6th, 2026 03:16 am
Powered by Dreamwidth Studios