in reply to Randomize word

Why expend all those cycles breaking the string into and array and then more cycles to stick'em all back together again :) (It's even fair!)

#! perl -slw use strict; sub shuffleWord { my( $l, $p ) = length( $_[0] )-1; $p = int rand $l - $_ and substr( $_[ 0 ], $_ , 1 ) ^= substr( $_[ 0 ], $_ + $p, 1 ) ^= substr( $_[ 0 ], $_ , 1 ) ^= substr( $_[ 0 ], $_ + $p, 1 ) for 0 .. $l; return $_[ 0 ]; } print shuffleWord $ARGV[ 0 ]; __END__ P:\test>test.pl antidisastablishmentarismmania teinisasdrslaathiaismmtabnnima P:\test>test.pl antidisastablishmentarismmania ibsrniitmsaaadnnaealttiimsmhsa P:\test>test.pl antidisastablishmentarismmania iiarsshaminaaittlmtnbasmsednia

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

Replies are listed 'Best First'.
Re^2: Randomize word
by go9kata (Novice) on Aug 30, 2013 at 08:08 UTC
    Last character never get changed

      Remove the -1:

      sub shuffleWord { my( $l, $p ) = length( $_[0] ); $p = int rand $l - $_ and substr( $_[ 0 ], $_ , 1 ) ^= substr( $_[ 0 ], $_ + $p, 1 ) ^= substr( $_[ 0 ], $_ , 1 ) ^= substr( $_[ 0 ], $_ + $p, 1 ) for 0 .. $l; return $_[ 0 ]; }

      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".
      In the absence of evidence, opinion is indistinguishable from prejudice.