Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^4: Txet Maglning Glof, Ayobndy? (timing)

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


in reply to Re: Re^2: Txet Maglning Glof, Ayobndy? (eagle)
in thread Txet Maglning Glof, Ayobndy?

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

Replies are listed 'Best First'.
Re^5: Txet Maglning Glof, Ayobndy? (both!)
by tye (Sage) on Sep 16, 2003 at 17:30 UTC

    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
      It appears that sort (Perl 5.006_00) treats ( -1 < values < +1 ) like "0"
      That'll be because of a conversion to integer, I guess.

        Duh. Thanks. (:

                        - tye

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2024-03-29 08:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found