in reply to Txet Maglning Glof, Ayobndy?


perl -pe 's/(\w)(\w+)(\w)/split"",$2;$,="";$,.=splice@_,rand@_,1while@ +_;$1.$,.$3/eg' file
Updated: I originally missed the part about keeping the last letter as per Cody Pendant's comment below. Also, some words will be randomly mangled back to the same word and words less than four letters won't be changed.

A shorter variation:

perl -pe 's/(?<=\w)\w+(?=\w)/split$,,$&;my$s;$s.=splice@_,rand@_,1whi +le@_;$s/eg' file

--
John.

Replies are listed 'Best First'.
Re^2: Txet Maglning Glof, Ayobndy? (eagle)
by tye (Sage) on Sep 16, 2003 at 06:20 UTC

    Two characters shorter (64):

    perl -pe " s#(\w)(\w+)(\w)#join$,,$1,sort({(-1,1)[rand 2]}split$,,$2),$3#ge" #12345678 1 2345678 2 2345678 3 2345678 4 2345678 5 2345678 6 234

    It should be possible to golf that down some more, but I'm late for bed. (: Have fun.

                    - tye
      So all but first and last must be reordered, but nobody said they must be randomly scrambled. 35
      perl -pe 's/(?<=\w)(\w+)(?=\w)/(reverse$1)/eg' file 12345678901234567890123456789012345

        Ditch efficiency and save four strokes: :-)
        perl -pe 's/(?<=\w)\w+(?=\w)/reverse$&/eg' file

        --
        John.

        Doh! And I guess we don't need no stinking look(ahead|behind)s either (22):
        perl -pe 's/\B\w+\B/reverse$&/eg' file
      Again, hmm, this one always gets the right letters on the ends as far as I can see, but doesn't necessarily mangle all words. I just got:

      Aircnodcg to a researhecr at an Enigslh urisnviety, it doesn't matter in what order the ltertes in a wrod are.

      in which quite a few words went unmangled.

      ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss') =~y~b-v~a-z~s; print

        Yes, your original spec (that my solution was written based on) had a requirement that "the rest of the letters must be out of order". I realized that this requirement was impossible so I chose to ignore it rather than implement an infinite loop (as you first did) or to ammend the rules in a different way (as you later did).

        Personally, I much prefer having the order be random over some fixed reorordering that meets a "must be out of order" rule but doesn't meet the spirit of the original problem, IMHO. Both "must change if possible" and "random" would be best, but if I pick only one of those two, then "random" is the clear choice to me (though the other choice certainly makes for shorter code). :)

                        - tye
      How about this:
      perl -pe 's#\B\w+\B#join$,,sort({(-1,1)[rand 2]}split$,,$&)#ge'
      That's 52 chars without the "perl -pe"
        perl -pe "s#\B\w+\B#join$,,so­rt{rand~0}split$,,$&#ge" # 12345678 1 2345678 2 2345678 3 2345678 4 2
                        - tye
Re: Re: Txet Maglning Glof, Ayobndy?
by Cody Pendant (Prior) on Sep 16, 2003 at 07:19 UTC
    Hmm, if I do this:
    $str = ' According to a researcher at an English university, it doesn\'t matter in what order the letters in a word are.'; $str =~ s/(\w)(\w+)/split"",$2;$,="";$,.=splice@_,rand@_,1while@_;$1.$ +,/eg; print $str;

    I get "Anogdcric to a reecasrreh at an Enghsli uyirenvist, it dnseo't matter in wtha oderr teh leettrs in a wdor are."

    Which doesn't fit the brief.

    Some words are not mangled, and nearly all of them have the wrong last letter.



    ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss') =~y~b-v~a-z~s; print