First, can you guess what the following code does before running it? ...
1x~@;

This node by shmem gave me the idea, and may give you a hint as well.

Now for the challenge ... what's the shortest program which does the same thing?  My guess is the obvious:

1x~0

with any digit in place of '0' working equally well, as does any digit except '0' exchanged for the '1'.

How about the smallest program that does the same thing slowly?  The following takes about 11 seconds on my laptop:

sub _{_()}_

I have no idea if a shorter such program exists.


s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

Replies are listed 'Best First'.
Re: Guess what it does ... and a challenge
by blokhead (Monsignor) on May 24, 2008 at 19:36 UTC
    I parsed that code correctly in my head, but didn't realize what the result would be.

    Here is a program of the same length (as your second snippet) that does the same thing slowly:

    1while$/x=8
    Changing "8" to "2" will presumably make it take a longer time, but I didn't have the patience. Also, presumably this guy will take exponentially longer to get there:
    1while$/.=1

    blokhead

      perl -e '1while++$_' eventually will eat all memory too... the most slower way :)

        No, it won't. Now, if you do $_='a' first, then it will, and extremely slowly too.

        - tye        

Re: Guess what it does ... and a challenge
by kyle (Abbot) on May 24, 2008 at 20:04 UTC

    I'm cheating a little, but...

    perl -e '<>' < /dev/zero

    That's two characters of Perl, but it only "works" with the right wrapping.

Re: Guess what it does ... and a challenge
by ambrus (Abbot) on May 25, 2008 at 11:40 UTC

    Without running it, I think it will

    die immediately but give some kind of intelligable error about too large a string repetition. This is because ~ in perl5 always gives an unsigned number, so ~@; is not -1 but 2**32-1 or 2**64-1. If that's wrong, my second guess is crashing without any message with a fatal signal, like SIGABRT or SIGSEGV.

    I've ran it. It

    died with the error message "Out of memory!". Note how unusual this error is: it doesn't have the file, line, and other usual info appended, and it has an excalmation mark.
Re: Guess what it does ... and a challenge
by shmem (Chancellor) on May 24, 2008 at 22:53 UTC
    First, can you guess what the following code does before running it?

    Oh yes...

    it allocates an insane amount of memory, which I don't have - therefore, I won't run it ;-)

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
golf! (now this title is more than one word)
by locked_user mtve (Deacon) on May 31, 2008 at 18:59 UTC
    perl -e 'sub _{&_}_'
Re: Guess what it does ... and a challenge
by ambrus (Abbot) on May 25, 2008 at 11:54 UTC

    This does it slowly and is one character shorter than the one you gave in your spoiler:

    1x~sleep 9

    Update: Btw one could try this (CAUTION! DON'T TYPE BLINDLY!):

    `yes|tac`
    but it doesn't work, because
    it fills up your disk instead. With GNU Coreutils, you can use the TMPDIR env-var to override where it puts its temporary files. Eg.
    [am]king ~$ TMPDIR=/dev/shm/ time perl -we '`yes|tac`' tac: /dev/shm//tacck0ml6: write error: No space left on device 11.48user 2.56system 0:14.05elapsed 99%CPU (0avgtext+0avgdata 0maxresi +dent)k 0inputs+0outputs (0major+1382minor)pagefaults 0swaps

    Update: On the other hand, this works, and is shorter than the first one:

    $x=`yes`
    (The first three characters are needed because otherwise the readpipe is called in void context so perl throws away what it reads right away.)

    Update: the following works too and is even shorter:

    1+`yes`

    Update: and one more character shorter is:

    -`yes`

    Update: the following has the same length as the first one in this node, but is a slower than that:

    1x~sleep$=