Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

(GOLF) Randomizing lines

by CheeseLord (Deacon)
on Jul 26, 2001 at 17:22 UTC ( [id://99962]=perlmeditation: print w/replies, xml ) Need Help??

The story: A few obfus ago I had a bunch of lines that I wanted to randomize to keep the obfu from being deconstructed a little too quickly. So, I came up with a short script that I have since shortened to a one-liner:

123456789_123456789_123456789_123456789_123456789_123456789_1 perl -e'@l=<>;while(@l){$i=rand@l;print$l[$i];splice@l,$i,1}'

Scored in accordance with PGA Golf Rules, it hits the clubhouse at 61 chars. However, I'm sure there's probably much shorter ways out there that my feeble, cheese-filled mind isn't thinking of, and I figured some of you might get a little enjoyment out of this, so here we go...

Sample input:

This is some sample input that CheeseLord put here for you.

Sample output:

input that for you. put here CheeseLord is some sample This

Final Note: The space between -e and ' has proven optional everywhere I've tried it -- but if it turns out not to be somewhere, I'll gladly take the one-stroke penalty.

His Royal Cheeziness

Replies are listed 'Best First'.
Re: (GOLF) Randomizing lines
by tadman (Prior) on Jul 26, 2001 at 17:39 UTC
    Err, 48 if you count the whole works:
    perl -e'@l=<>;print while($_=splice@l,rand@l,1)' </CODE>
    Update:
    Best beat myself before someone else does. As often occurs, map inspires and produces useful results. 46 characters:
    perl -e'map{splice@l,rand@l+1,0,$_}<>;print@l'
    Or, if you really want to get down to brass tacks, 45 characters:
    perl -e'splice@l,rand@l+1,0,$_ for<>;print@l'
    Note that the '+1' can be eliminated if there is a blank line at the start of the file.

    Update 2.0
    It occured to me to break sort and make it act all screwy, which sure jumbles up the text a fair bit, for all intents appearing totally random. 31 characters:
    perl -e'print sort{4-rand 9}<>'
      Version 2, aside from the obvious defect of needing to be adjusted to your file, is rather dangerous. It can dump core on older Perls, and will be biased even on recent ones. (I think you knew that, but I though it worth pointing out.)
        I'm not sure what you mean by "adjusted to your file", as it seems to work quite well on any given data. It isn't perfectly random, as things aren't shuffled quite as much as the splice approach, but they are shuffled to the point of being random-looking with no obvious patterns. Since we're not picking winning lottery numbers, I thought that would be acceptable.

        I can only presume that since the output of the comparator used by sort does return different values for the same $a-$b pair, this could really bust a gasket on some of the older Perls which perhaps assume that this would not occur. IMHO, this is a bug in Perl more than it is a bug in the program, as no program is supposed to be able to dump core, unless, perhaps, it uses the dump command.
Re: (GOLF) Randomizing lines
by jmcnamara (Monsignor) on Jul 26, 2001 at 17:54 UTC

    This will always randomise in the same way, which probably isn't what you want. Also, some lines might be lost if the file contains duplicate lines. However it is short.

    22 chars:
    perl -e'%_=<>;print%_' file


    John.
    --

      Then why not:perl -e'print%{{<>}}' ?  (21 chars )
      (and it's pretty), (and, as with the other, it even gets rid of duplicate odd-numbered lines (and the following line) for you.)

        p

Re (tilly) 1: (GOLF) Randomizing lines
by tilly (Archbishop) on Jul 26, 2001 at 17:47 UTC
    45 (oops, didn't score according to the rules the first time, thanks CheeseLord) after some usual compression.
    123456789 123456789 123456789 123456789 12345 perl -e'@l=<>;print splice@l,rand@l,1while@l'
    UPDATE
    Does anyone have any ideas why I get a segmentation fault from the following?
    perl -e'print splice@l,rand@l,1for@l=<>'
      Your segfaults are probably related to the bug I noted in Segfaulty. Namely, the for loop is iterating over the aliased values of @l, which you are deleting during the course of the loop. Not very nice :)

      Thus it works at 43 chars, if you do:

      perl -e'print splice@l,rand@l,1for@m=@l=<>' t.txt
         MeowChow                                   
                     s aamecha.s a..a\u$&owag.print
      That's really odd. I tried running it in the debugger:
      $ perl -d -e'print splice@l,rand@l,1for@l=<>' < test Default die handler restored. Loading DB routines from perl5db.pl version 1.07 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(-e:1): print splice@l,rand@l,1for@l=<> DB<1> c 4 5 1 3 Attempt to free unreferenced scalar at -e line 1, <> line 5. Bus error (core dumped)
      Another time (while stepping through with 'n'), I got this error:
      Attempt to free unreferenced scalar at /usr/local/perl/lib/5.6/perl5db +.pl line 1447, <IN> line 7.
      Yet another time, I stepped through with 'n' and it worked fine.

      Potentially a bug in perl?

Re: (GOLF) Randomizing lines
by dragonchild (Archbishop) on Jul 26, 2001 at 17:50 UTC
    I've got it down to 47:

    123456789_123456789_123456789_123456789_1234567 perl -e'@l=<>;print splice(@l,rand@l,1)while@l'
Re: (GOLF) Randomizing lines
by Cirollo (Friar) on Jul 26, 2001 at 17:56 UTC
    This doesn't really "randomize" it, becuase one input will result in the same output every time. But, it does shuffle things up. 22 characters.

    perl -e'%h=<>;print%h'

    Update: D'oh! jmcnamara beat me to the same thing! Great minds think alike ;)

(MeowChow) Re: (GOLF) Randomizing lines
by MeowChow (Vicar) on Jul 26, 2001 at 23:55 UTC
    39 chars:
      
    perl -ne'splice@l,rand$.,0,$_}{print@l'
    And here's a nicely obfuscated one at 49:
      
    perl -ne'@=[~~@=,$=]=($=[$==rand$.],$_)}{print@='
       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print
Re: (GOLF) Randomizing lines
by bwana147 (Pilgrim) on Jul 26, 2001 at 19:35 UTC

    Hey, here's another shot at 37 characters:

    perl -e'print sort{int rand 2or-1}<>'

    Update: oops! I hadn't seen tadman's Update 2.0... I'm too late :-(

    --bwana147

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2024-04-20 01:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found