Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: Txet Maglning Glof, Ayobndy? (eagle)

by tye (Sage)
on Sep 16, 2003 at 06:20 UTC ( [id://291732]=note: print w/replies, xml ) Need Help??


in reply to Re: Txet Maglning Glof, Ayobndy?
in thread Txet Maglning Glof, Ayobndy?

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

Replies are listed 'Best First'.
Re: Re^2: Txet Maglning Glof, Ayobndy? (eagle)
by Anonymous Monk on Sep 16, 2003 at 07:43 UTC
    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
Re: Re^2: Txet Maglning Glof, Ayobndy? (eagle)
by Cody Pendant (Prior) on Sep 16, 2003 at 07:36 UTC
    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

        I came up with a way of doing both "random" and "must change" that I liked so I golfed it a bit while still keeping it strict and warnings compliant (in spirit, even):

        #!/usr/bin/perl -p #12345678 1 2345678 2 2345678 3 2345678 4 2345678 5 2345678 s#\B(\w+)\B#join$,,(split$,,$1)[sub{my($l,@x)=@_;$l=$x[$l] =pop while@_;@x}->(sort{(-1,1)[rand 2]}0..-1+length$1)]#ge # 6 12345678 7 2345678 8 2345678 9 2345678 0 2345678 1 2345
        But 115 characters is rather long. Trying to golf harder, I only saved 7 strokes (108):
        #!/usr/bin/perl -p #12345678 1 2345678 2 2345678 3 2345678 4 2345678 5 2345 s#\B\w+\B#join$,,(split$,,$&)[sub{($b,@*)=@_;$b=$*[$b]= pop while@_;@*}->(sort{(-1,1)[rand 2]}0..length$&)]#ge #678 6 12345678 7 2345678 8 2345678 9 2345678 0 2345678

        Anyway, I find the algorithm interesting so you might treat it like an obfu and deconstruct it. (:

        I'm also curious why I need to do:

        sort {(-1,1)[rand 2]} ...
        Shouldn't the following do the same thing (other than very, very rarely returning "0" for the comparison)?
        sort {-.5+rand} ...
        It appears that sort (Perl 5.006_00) treats ( -1 < values < +1 ) like "0" [I expected -0.1402 to be treated like "-1" and +0.3886 like "+1"] but treats ( values <= -1 ) like "-1" and ( 1 <= values ) like "+1".

        I find this rather strange. It is also quite unfortunate when golfing. :)

        Update: Thanks to PhilHibbs' insight, I realized I could golf further by taking advantage of a different "convert to IV" side-effect:

        #!/usr/bin/perl -p #12345678 1 2345678 2 2345678 3 2345678 4 2345678 5 23 s#\B\w+\B#join$,,(split$,,$&)[sub{($b,@*)=@_;$b=$*[$b ]=pop while@_;@*}->(sort{rand~0}0..-1+length$&)]#ge #45678 6 12345678 7 2345678 8 2345678 9 2345678 0 23
        which gets me to 103 strokes (I also realized that dropping "-1+" made it possible for words to stay unchanged).

                        - tye
Down to 52
by Lorphos (Novice) on Sep 18, 2003 at 15:29 UTC
    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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://291732]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-25 23:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found