in reply to Shuffling CODONS

My favorite shuffle

#!/usr/bin/perl # http://perlmonks.org/?node_id=1216102 use strict; use warnings; my @trips = 'a' .. 'z'; my @shuftrips = myshuffle( @trips ); print "@shuftrips\n"; sub myshuffle { map $_->[0], sort { $a->[1] <=> $b->[1] } map [ $_, rand ], @_; }

Replies are listed 'Best First'.
Re^2: Shuffling CODONS
by BrowserUk (Patriarch) on Jun 07, 2018 at 14:41 UTC

    Why is that a favorite? I get it'll shuffle anything correctly, but trading O(NlogN) for O(1) is painful:

    Benchmark:

    Rate tybalt buk1 buk2 ListUtil tybalt 224/s -- -65% -70% -97% buk1 644/s 187% -- -14% -91% buk2 746/s 233% 16% -- -90% ListUtil 7342/s 3176% 1040% 884% -- C:\test>shufflesBenchmark2018.pl -SIZE=1e3 Rate tybalt buk1 buk2 ListUtil tybalt 229/s -- -66% -70% -97% buk1 675/s 195% -- -13% -91% buk2 776/s 238% 15% -- -89% ListUtil 7339/s 3102% 987% 846% -- C:\test>shufflesBenchmark2018.pl -SIZE=1e4 Rate tybalt buk1 buk2 ListUtil tybalt 17.2/s -- -75% -78% -98% buk1 67.5/s 292% -- -13% -90% buk2 77.2/s 349% 14% -- -89% ListUtil 704/s 3992% 944% 812% -- C:\test>shufflesBenchmark2018.pl -SIZE=1e5 (warning: too few iterations for a reliable count) Rate tybalt buk1 buk2 ListUtil tybalt 1.21/s -- -81% -84% -98% buk1 6.40/s 430% -- -15% -89% buk2 7.53/s 523% 18% -- -87% ListUtil 56.4/s 4566% 781% 649% --

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
    In the absence of evidence, opinion is indistinguishable from prejudice. Suck that fhit
Re^2: Shuffling CODONS
by WouterVG (Novice) on Jun 07, 2018 at 14:02 UTC

    Thanks for your reply! If I implement it like below. It doesn't change the order of codons.

    print "enter sequence and signal end with enter followed by ctrl d\n" +; $sequence = <STDIN>; chomp $sequence; print "sequence inserted : $sequence\n"; @trips = unpack("a3" x (length($sequence)-2), $sequence); @trips = join(" ", @trips); my @shuftrips = &myshuffle( @trips ); print "@shuftrips\n"; sub myshuffle { map $_->[0], sort { $a->[1] <=> $b->[1] } map [ $_, rand ], @_; }

      Because @trips = join(" ", @trips); joins all codons into ONE string!

        ^^^ THIS