Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Decimal Gray Code in 31 chars

by Oromis92 (Sexton)
on Jan 18, 2010 at 22:14 UTC ( #818060=obfuscated: print w/ replies, xml ) Need Help??

print(($_^$_>>1).$/)for(0..pop)

simplest as possible

Comment on Decimal Gray Code in 31 chars
Download Code
Re: Decimal Gray Code in 31 chars
by ikegami (Pope) on Jan 18, 2010 at 22:25 UTC

    From your 35

    perl -e'print(($_^$_>>1).$/)for(0..pop)' 15 12345678901234567890123456789012345

    I shave 6 off

    perl -le'print$_^$_>>1 for 0..pop' 15 12345678901234567890123456789

    3 more if 5.10+

    perl -E'say$_^$_>>1 for 0..pop' 15 12345678901234567890123456

      two more:

      print$_^$_/2for 0..pop
        I swear I tried removing that space and got an error. But yeah, it works.

      It's funny how some people still post nodes with such titles despite the warning on the top of Obfuscated code: Word of warning, though: Don't be too cocky with your post almost inevitably someone will post a reply that does the exact same thing in even fewer characters!

        My guess is because by saying "simplest as possible", it challenges others to improve on it ;). Do you think this many comments would be gathered otherwise?
Re: Decimal Gray Code in 31 chars
by tweetiepooh (Friar) on Jan 25, 2010 at 17:11 UTC
    Meybe I'm not seeing something but I get

    0132675412131514101198
      What do you expect to get? Here's what I get when I run the OP's code:
      perl -e 'print(($_^$_>>1).$/)for(0..pop)' 15 0 1 3 2 6 7 5 4 12 13 15 14 10 11 9 8
      Are you questioning why you have no newlines? That is puzzling. Did you do this undef $/; by any chance?

      Or, are you questioning the sequence of values? Maybe looking at the Gray_code in the binary format makes it more obvious:

      perl -e 'printf("%04b$/",($_^$_>>1))for(0..pop)' 15 0000 0001 0011 0010 0110 0111 0101 0100 1100 1101 1111 1110 1010 1011 1001 1000
      You might be comparing the output you get from the program with the strings in my node. The strings I posted aren't the output I got, but an indication of the program size.

      I also get for:

      perl -E'print$_^$_/2for 0..pop' 15

      Output:

      0132675412131514101198

      But for:

      perl -E'say$_^$_/2for 0..pop' 15

      I get:

      0 1 3 2 6 7 5 4 12 13 15 14 10 11 9 8

      I believe that was mtve's intent. Um, that's 20 chars of actual code.

        Actually my guess is rather perl -le'print$_^$_/2for 0..pop'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: obfuscated [id://818060]
Approved by ikegami
Front-paged by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2014-10-30 23:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (211 votes), past polls