The Daily WTF: Curious Perversions in Information Technology
Welcome to TDWTF Forums Sign in | Join | Help
in Search

Make a program that still runs if the lines are shuffled

Last post 01-25-2010 5:25 AM by Shadikka. 23 replies.
Page 1 of 1 (24 items)
Sort Posts: Previous Next
  • 02-18-2008 12:00 AM

    Make a program that still runs if the lines are shuffled

    This is a new challenge. Make a program that still runs if the lines are shuffled. Another challenge is to make a program that still runs if the lines are shuffled and that one line at random might be taken out. Lines cannot be longer than 40 characters each. Use any program language, or preferably write it in multiple program languages to see which is easy and which are harder to do it. See if you can answer any of the other coder challenges on this web site using these harder challenges.

    : IF` 0=GOTO` ORIG ;
    : THEN` HERE SWAP ! ;
    : ELSE` GOTO` ORIG SWAP THEN` ;


    gopher://zzo38computer.cjb.net/
  • 02-18-2008 6:20 AM In reply to

    • Daid
    • Top 75 Contributor
    • Joined on 01-30-2007
    • Posts 348

    Re: Make a program that still runs if the lines are shuffled

    I guess basic with labels and gotos would work? Or isn't it allowed to have a label and some code on the same line in basic? (Woot, my basic is getting rusty. Sanity is near!)

  • 02-18-2008 6:48 AM In reply to

    • ammoQ
    • Top 10 Contributor
    • Joined on 04-13-2005
    • Vienna.Austria.Europe.Earth
    • Posts 3,444

    Re: Make a program that still runs if the lines are shuffled

    Daid:

    I guess basic with labels and gotos would work? Or isn't it allowed to have a label and some code on the same line in basic? (Woot, my basic is getting rusty. Sanity is near!)

     

    Hmm... using the right version of BASIC, almost everything should be possible...

    aa:x=1:goto ab
    ab:if x:print "worse":goto ac
    ac:if x:print "than":goto ad
    ad:if x:print "failure":end

    beanbag girl 4ever
  • 02-18-2008 10:01 AM In reply to

    Re: Make a program that still runs if the lines are shuffled

    [root@Fonz1 ~]# cat test.c 
    int main(){phello();return 0;}
    void phello(){puts("Hello World!");}
    #include <stdio.h>
    [root@Fonz1 ~]# gcc test.c && ./a.out
    test.c:2: warning: conflicting types for ‘phello’
    test.c:1: warning: previous implicit declaration of ‘phello’ was here
    Hello World!
    
    Yay for C's implicit declarations!
    irc://irc.slashnet.org/#TDWTF (Redirects to #CodeLove )
    Yo dawg I herd hoard you like to search so I put a 2TB txt file in yo SSDS so your memory's maxed out and your computer cant do shit? -- Nyquist
  • 02-20-2008 1:03 PM In reply to

    Re: Make a program that still runs if the lines are shuffled

    I did it in Ruby.

    puts "that was easy"
    
    "Frames securely mediate, by design. Secure multi-mediation is the future of all webbing."
  • 02-20-2008 3:51 PM In reply to

    Re: Make a program that still runs if the lines are shuffled

    djork:

    I did it in Ruby.

    puts "that was easy"
    
    Wow, and it's even multilingual! You rullz0rs.
    ╩юфют√ь ёЄЁрэшЎрь яюЁр эр яхэёш■.

    #TDWTF @ SlashNET was merged into #codelove @ the same network. You're still welcome to drop by. I guess.
  • 02-21-2008 2:14 PM In reply to

    Re: Make a program that still runs if the lines are shuffled

    Try to make more complicated programs that work OK when the lines are shuffled, possibly with even shorter max length (such as 30 or 20 even) and see which program languages it is easy and which it is harder. Can you make it in C, C++, Perl (not counting shebang line), PHP (not counting the <?php line), Brainfuck, Unlambda, DOS Batch file, javascript, etc? Also see it one program can work when one line is removed at random, I read somewhere that when they used to make program on punch cards the French security people, not being programmers, removed some of the cards at random to inspect them, and of course the program didn't work. But can you make a program that still works even if some of the cards are removed at random? Probably not, but it might be interesting to try.
    : IF` 0=GOTO` ORIG ;
    : THEN` HERE SWAP ! ;
    : ELSE` GOTO` ORIG SWAP THEN` ;


    gopher://zzo38computer.cjb.net/
  • 02-21-2008 4:22 PM In reply to

    Re: Make a program that still runs if the lines are shuffled

    C++ is out, it doesn't have C's implicit declaration. IIRC Python and PHP both require the function to be declared before it can be used, unless you do something like the ruby example.
    irc://irc.slashnet.org/#TDWTF (Redirects to #CodeLove )
    Yo dawg I herd hoard you like to search so I put a 2TB txt file in yo SSDS so your memory's maxed out and your computer cant do shit? -- Nyquist
  • 02-21-2008 4:43 PM In reply to

    • lidden
    • Not Ranked
    • Joined on 02-06-2008
    • Posts 2

    Re: Make a program that still runs if the lines are shuffled

    In Perl you can do like this: 

    sub f1{print @_ if!$i++}
    sub f2{print @_ if!$i++}
    eval{f1("Hello World!\n")};
    eval{f2("Hello World!\n")};

    It handles shuffling and removal of one line.

    Extending to more complicated programs is left as an exercise for the reader :-)

     

  • 02-21-2008 5:59 PM In reply to

    • mfah
    • Top 500 Contributor
    • Joined on 12-01-2007
    • Posts 117

    Re: Make a program that still runs if the lines are shuffled

    Slightly cheating as it doesn't actually do anything, but here's a VBScript example:

    i = 0
    j = 0
    x = 0
    y = 666
    z = 27
    w = 48
  • 02-27-2008 3:37 PM In reply to

    • zokar
    • Not Ranked
    • Joined on 02-27-2008
    • Posts 3

    Re: Make a program that still runs if the lines are shuffled

     I should point out that this was done in IOCCC, with the added benefit that many of the shuffled programs behave differently. All are valid however.

     It isn't resilient against dropping certain lines (e.g. the line containing main) but will work if shuffled in any order.

    Also, the ??> and ??< things aren't mistakes. They're C trigraphs -- ??< is {, ??> is }.

    Reference: 2001/westley.c

     

    main(i)??<char c??(81??)??(81??);for(i=0;gets(*(c+i)); i++);A(i,c);J(i-1,c);??>
    /* { /KC 0000 K } */
    S(c,r,i)char*c; ??<printf("|");for(;*(c+i); i++)putchar(T(*(c+i),r));U(c,i);??>

    char* B,*r; X(c,k)??<return isupper(c)?0:V(*(r+k),B); ??>
    J(i,c,j)char*c; ??<for(j=t(i,0) ; j+j<i; j++)L(c,j,i-j); M(i,c); ??>
    U(c,i)char*c; ??<while(i++<80)printf(" "); puts("|"); ??>

    A(i ,c,j ,k) char *c; ??< for(j= -1; ++j<i; )for (k =j; k<i; )D(c ,j, k++); ??>
    R(c,r) char *c; ??< printf( "/%-80s|??/n" ,c); for (; r< 13; r++)S (c,r,0); ??>
    t(a,b)??<return a;??>

    M(i,c)char c??(81??)??(81??);??<i++;while(i--)t(1,0)?puts(*(c+i)):P(*(c+i));??>
    char*d= "yyzzyzz zz z0y000 zz 00yzz00y00yzz Brian Westley zyy00yy0yy 0" ; ; ;
    #define t(a,b) t(b,a)

    char* K;Y (c ,r) ??< int i=V ( tolower (c) ,K); return(i <0)?1 :Z(c ,r, i); ??>
    char*m= "7831734578 9547693564569528334164263z284106579 52626423135477"; ; ; ;
    P(c)char*c; ??<Q(1,0); R(c,1 ); Q(0,0); ??>

    char* d,*m,*B; Z(c,i,k)??<return W(i, V(*(d+k), B),V(*( m+k),B) ,X(c, k)); ??>
    char*K= "gh$j|lm'pq&rv<x>z#);@_winky,.%/+*!=3-284106579 (:obfuscated??/?" ; ;
    L(c,i,j)char c??(81??)??(81??);??<G(* (c+i),*(c+j));??>
    char*r= "008y8yy8yyyyz8z8z88888z0yyz888 8888 88y00zz00z0088";
    D(c,i,j) char c??(81??)??(81??);??<E(*(c+i),*(c+j));??>

    E(r,s)char*r,*s; ??< if(t( 0,1)&& F(r,s)<0)G(r,s); ??>
    H()??<static i=0; if (!i) ??< time(&i); srand(i); ??> return rand()-rand(); ??>
    F(r,s)char*r,*s; ??<return t(0,1)?H():strcmp(r,s); ??>

    T(c,r) ??< return Y(c, r)?'??(' :' '; ??>
    G(r,s)char*r,*s; ??<char t??( 81??); strcpy(t,r); strcpy(r,s); strcpy(s,t); ??>
    W(r,a,b,c)??<return r==a||r==b||r==c; ??>

    Q(t,i)??< printf(t?" ":"|"); for(; i<80; i++) printf("_"); puts(t?"_":"|"); ??>
    char*B=" yz0123456789";
    V(c,k,i) char*k; ??<for (i=0; *(k+i) &&*(k+i)!=c; i++); return *(k+i)?i:-1; ??>
  • 02-28-2008 3:53 AM In reply to

    Re: Make a program that still runs if the lines are shuffled

    Lingerance:
    IIRC Python and PHP both require the function to be declared before it can be used
     

    Not quite correct for PHP. Functions can be called before their declaration in the script -- as long as they're declared in global scope. A curious kind of retardation causes PHP to define functions in a deeper scope in the place they're declared in.

    consider this code:

    foo(); //success; foo() is declared in gobal scope
    bar(); //fail, obviously. bar() is not in this scope. No surprise here.

    function foo() {
       echo "dolt";

       bar(); //FAIL. bar() is not yet defined.

       function bar() {
          echo "wtf";
       }
       bar(); //success.
    }

     

    — Flurp.
  • 03-26-2008 12:34 PM In reply to

    • deibyz
    • Not Ranked
    • Joined on 03-26-2008
    • Posts 3

    Re: Make a program that still runs if the lines are shuffled

     I´m a bit late, but here is mine. You can make it do almost anything with all the lines shuffled as much as you want:

    $ cat bar.pl 

    #3: print $a;
    #1: my $a = "The Daily";
    #2: $a .= " WTF!\n";
    seek DATA,0,0;@a=<DATA>;eval(join$/,map{(split ":")[1]}sort@a[0..$#a-1]);__DATA__
     

    $ perl bar.pl
    The Daily WTF!
     

  • 04-04-2008 2:50 PM In reply to

    Re: Make a program that still runs if the lines are shuffled

    Doing this in functional languages is quite easy lol. Here's one in Haskell:

    readInt :: String -> Int

    readInt = read

    fib 1 = 1

    fib 2 = 1

    fib n = fib(n-1) + fib(n-2)

    main = getLine >>= readInt >>= return . fib >>= (\x -> putStrLn("answer: " ++ (show x)))

     

    It should work with any shuffling of lines, but doesn't support a random deletion of lines. No idea how to support deletion. 

    ((lambda (f) (f f)) (lambda (f) (f f)))
  • 04-04-2008 2:58 PM In reply to

    Re: Make a program that still runs if the lines are shuffled

    HypocriteWorld:
    No idea how to support deletion. 
    Only way I can think of is to have two lines that will check a state and do an action if the state is X but not if it is Y, then set the state to Y. Example: (PHP not tested)
    if (!isset($state)) $state = 1; action(); //Firefox says isset is spelt correctly
    if (!isset($state)) $state = 1; action(); //Firefox says isset isn't a word, WTF.
    
    Also you could probably cheat and say: my program has a 95% chance of working with a line missing because 95% of the lines don't do anything.
    irc://irc.slashnet.org/#TDWTF (Redirects to #CodeLove )
    Yo dawg I herd hoard you like to search so I put a 2TB txt file in yo SSDS so your memory's maxed out and your computer cant do shit? -- Nyquist
  • 04-04-2008 3:54 PM In reply to

    Re: Make a program that still runs if the lines are shuffled

     

    Lingerance:
    HypocriteWorld:
    No idea how to support deletion. 
    Only way I can think of is to have two lines that will check a state and do an action if the state is X but not if it is Y, then set the state to Y. Example: (PHP not tested)

    if (!isset($state)) $state = 1; action(); //Firefox says isset is spelt correctly
    if (!isset($state)) $state = 1; action(); //Firefox says isset isn't a word, WTF.

    Also you could probably cheat and say: my program has a 95% chance of working with a line missing because 95% of the lines don't do anything.

    Too bad Haskell is a stateless language... so probably not a good language for the "delete one line and still run" stuff...there's practically no way to prevent the special case of the "main = ..." line being deleted, for example

    ((lambda (f) (f f)) (lambda (f) (f f)))
  • 07-09-2008 1:23 PM In reply to

    • Ren
    • Top 500 Contributor
    • Joined on 07-04-2008
    • Posts 64

    Re: Make a program that still runs if the lines are shuffled

    sub t{$_='ebcda fghij';&i;}
    sub h{return defined($_);}
    sub i{s/e/H/;&s if h $_;}
    sub s{s/b/e/;&b if h $_;}
    sub b{s/c/l/;&r if h $_;}
    sub r{s/d/l/;&l if h $_;}
    sub l{s/a/o/;&o if h $_;}
    sub o{s/f/w/;&e if h $_;}
    sub e{s/g/o/;&a if h $_;}
    sub a{s/h/r/;&z if h $_;}
    sub z{s/i/l/;&y if h $_;}
    sub y{s/j/d/;print;}

    &t;

     

    27 max linesize. And no, it doesn't matter where &t is. ;)

  • 07-09-2008 1:30 PM In reply to

    • Ren
    • Top 500 Contributor
    • Joined on 07-04-2008
    • Posts 64

    Re: Make a program that still runs if the lines are shuffled

    sub t{$_='ebcda fghij';&h;}
    sub h{s/e/H/;&i if defined $_;}
    sub i{s/b/e/;&s if defined $_;}
    sub s{s/c/l/;&b if defined $_;}
    sub b{s/d/l/;&e if defined $_;}
    sub e{s/a/o/;&z if defined $_;}
    sub z{s/f/w/;&y if defined $_;}
    sub y{s/g/o/;&r if defined $_;}
    sub r{s/h/r/;&o if defined $_;}
    sub o{s/i/l/;&x if defined $_;}
    sub x{s/j/d/;print;}

    Meh, couldn't edit the post anymore. Fix: sub h{} doesn't work if it's not defined before the rest. Max linesize grows to 31.

  • 07-11-2008 5:54 AM In reply to

    Re: Make a program that still runs if the lines are shuffled

    This one outputs the same text no matter how you shuffle it. It won't print anything if you remove a line, but it's impossible to make a brainfuck program crash.

    +>>>>>>>>>+++++++++[-<++++++++++++>]<<<<<<<<<------------[<]>>[.>>]<++++++++++++
    +>>>>>>>>>>>>>>>> >++++++++++[-<+++++++++++>]<+<<<<<<<<<<<<<<<<------------[<]>>[.>>]<++++++++++++
    +>>>>>>>>>>>>>++++[-<++++++++>]<<<<<<<<<<<<<------------[<]>>[.>>]<++++++++++++
    +>>>>>>>>>>>++++++++++[-<+++++++++++>]<+<<<<<<<<<<------------[<]>>[.>>]<++++++++++++
    +>>>>>>>>>>>>>>>>>>>>>+++++++++[-<++++++++++++>]<<<<<<<<<<<<<<<<<<<<<------------[<]>>[.>>]<++++++++++++
    +>>>>>>>>>>>>>>>>>>>++++++[-<+++++++++++++++++++>]<<<<<<<<<<<<<<<<<<<------------[<]>>[.>>]<++++++++++++
    +>>>++++++++[-<+++++++++>]<<<------------[<]>>[.>>]<++++++++++++
    +>>>>>>>>>>>>>>>>>>>>>>>> >++++[-<++++++++>]<+<<<<<<<<<<<<<<<<<<<<<<<<------------[<]>>[[.>>]]<++++++++++++
    +>>>>>++++++++++[-<++++++++++>]<+<<<<------------[<]>>[.>>]<++++++++++++
    +>>>>>>>+++++++++[-<++++++++++++>]<<<<<<<------------[<]>>[.>>]<++++++++++++
    +>>>>>>>>>>>>>>>++++++++++[-<++++++++++++>]<-<<<<<<<<<<<<<<------------[<]>>[.>>]<++++++++++++
    +>>>>>>>>>>>>>>>>>>>>>>>++++++++++[-<++++++++++>]<<<<<<<<<<<<<<<<<<<<<<<------------[<]>>[.>>]<++++++++++++
    rpar PROTON all
  • 10-02-2008 3:20 PM In reply to

    Re: Make a program that still runs if the lines are shuffled

    This is a little necro, but I just got back recently and this was a good one.  In C (has to accept C++ style comments, etc.) (you get some warnings, but it works); always the same and will work if any random line is deleted.  Can be any number of lines long as long as there are at least two lines

    main(int c,char**v){printf("z");}//\
    main(int c,char**v){printf("z");}//\
    main(int c,char**v){printf("z");}//\
    main(int c,char**v){printf("z");}//\
    main(int c,char**v){printf("z");}//\
    
  • 12-11-2008 1:16 PM In reply to

    Re: Make a program that still runs if the lines are shuffled

    This isn't really an answer, but it's not complicated at all to achieve this using _almost_ any programming language: just define each function on it's own line.

    Even better, write the whole program on a single line. You can try to shuffle it as much as you want and you'll still get the same thing.

    I would like to change the rules of this thread to: each implementation should have at least three lines that affect the result, where an implementation can be a function or the whole program (just to be safe, if the language doesn't support functions) and the program should have the same output.

    This would be invalid:

        function f() { a=1; b=a*2;

        x=0;

        return a; }

    Because x=0 doesn't really affect the result of the function.

    Now this might be a bit of a challenge to some. Good luck!

    and I love prepositions or
  • 11-01-2009 1:06 AM In reply to

    Re: Make a program that still runs if the lines are shuffled

    rohypnol:

    This isn't really an answer, but it's not complicated at all to achieve this using _almost_ any programming language: just define each function on it's own line.

    Even better, write the whole program on a single line. You can try to shuffle it as much as you want and you'll still get the same thing.

    I would like to change the rules of this thread to: each implementation should have at least three lines that affect the result, where an implementation can be a function or the whole program (just to be safe, if the language doesn't support functions) and the program should have the same output.

    This would be invalid:

        function f() { a=1; b=a*2;

        x=0;

        return a; }

    Because x=0 doesn't really affect the result of the function.

    Now this might be a bit of a challenge to some. Good luck!

     

    This should do it.

    static class Program { static void main(string[ args) { jclass Jclass = new jclass(); sclass Sclass = new sclass(); Console.WriteLine(Jclass.GetHello() + Sclass.GetWorld()); } 
    class jclass { public string GetHello(){return "Hello "; } }
    class sclass { public string GetWorld(){return "World!"; } }
  • 11-05-2009 10:18 PM In reply to

    Re: Make a program that still runs if the lines are shuffled

    Faxmachinen:

    This one outputs the same text no matter how you shuffle it. It won't print anything if you remove a line, but it's impossible to make a brainfuck program crash.

    ...snip...

     

    I personally like Brainfuck just because of the sheer obscurity.

  • 01-25-2010 5:25 AM In reply to

    • Shadikka
    • Not Ranked
    • Joined on 09-26-2007
    • Finland
    • Posts 3

    Re: Make a program that still runs if the lines are shuffled

    The largest challenge was thinking of a way to not get NameErrors from undeclared variables - luckily Python's builtin functions are quite replaceable. Sure, this is just a pretty boring implementation (it's a pretty simple if-then-structure a bit obscured), but anyway:

    abs=' !,Hdehilortw'
    zip=[3,5,8,8,9,2,0,12,9,10,8,4,1]
    print("".join(map(lambda x:(abs if type(abs) == str else 'idhbauorls,et ')[x], zip if type(zip) == list else ([3,7,0,11,6,5,10,5,1] if type(abs) == str else [3,6,7,11,1,13,12,6,13,1,11,4,12,2]))))

    Filed under:
Page 1 of 1 (24 items)
Powered by Community Server (Non-Commercial Edition), by Telligent Systems