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.
This account has disabled anonymous posting.
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

If you are unable to use this captcha for any reason, please contact us by email at support@dreamwidth.org

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 01:09 am
Powered by Dreamwidth Studios