Guess the code!

This is a discussion on Guess the code! within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by Mario F. Of course, this new type CClass is an oddity since it becomes possible to write ...

  1. #31
    Registered User
    Join Date
    Apr 2006
    Posts
    2,017
    Quote Originally Posted by Mario F.
    Of course, this new type CClass is an oddity since it becomes possible to write things like:

    +val = 13;
    I'd say it's an oddity because it overides opperator+() at all. For large object it would makes sence to return a reference, instead of copying the object, unless the object is changed when operator+ is called.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  2. #32
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Okey, my turn now:

    Code:
    y > x ?
    x >= 0 && y <= 1 :
    x <= 1 && y >= 0
    And my other still not solved problem in a reduced form:
    Code:
    const double h = 0.001;
    std::complex<double> z(1.0, 0.0), f(1.0, h);
    int i = 0;
    do {z *= f; i++;} while (imag(z) > 0);
    printf("%1.9lf\n", i*h - imag(z)/real(z));
    Last edited by TriKri; 11-07-2006 at 02:26 PM.

  3. #33
    Registered User
    Join Date
    Apr 2006
    Posts
    2,017
    I answered two of these so Here's mine.

    Write this function without recusion. (Thereby making apparent what the function does.)
    Code:
    char whatDoIDo(char (*func)(char, char), char a, const char * xs)
    {
        return *xs ? whatDoIDo(func, func(a, *xs) , xs+1) : a;
    }
    Last edited by King Mir; 11-08-2006 at 08:11 AM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #34
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by TriKri
    Aha, it is the maximum number of characters an integer type itype can require! I didn't see the definition was a macro until I noticed the \ sign, hehe. So I guess 12655/42039 is an approximation of log(2) then, it's pretty close.
    Bingo.

    Quote Originally Posted by TriKri
    Okey, my turn now:

    Code:
    y > x ?
    x >= 0 && y <= 1 :
    x <= 1 && y >= 0
    Result is true if x and y are in [0,1], false otherwise; an obfuscated way of writing this?
    Code:
    (x == 0 || x == 1) && (y == 0 || y == 1)
    Quote Originally Posted by TriKri
    And my other still not solved problem in a reduced form:
    Code:
    const double h = 0.001;
    std::complex<double> z(1.0, 0.0), f(1.0, h);
    int i = 0;
    do {z *= f; i++;} while (imag(z) > 0);
    printf("%1.9lf\n", i*h - imag(z)/real(z));
    An approximation of pi?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #35
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    The below two programs are based on a common theme.

    Code:
    unsigned i, j;
    for(i = 64; i; --i) {
        for(j = 0; j < 64; ++j)
            putchar("-#"[!(i - 1 & j)]);
        putchar('\n'); }

    This one is completely incomprehensible. The program is summarized in a one-word C-style comment which makes the last line exactly 80 characters long ("10-letters" is a placeholder); you can try guessing it instead

    Code:
    int main(){char*c=calloc(102,1);++c;*c+=8;while(*c){--*c;--c;*c+=4;c+=2;*c+=4;++
    c;++*c;c-=2;}c+=2;*c+=2;++c;*c+=2;c-=4;while(*c){--*c;while(*c){--*c;++c;++*c;--
    c;}++c;while(*c)--*c,++c,putchar(*c),c-=2,++*c,++c;c+=3;while(*c){--*c;while(*c)
    --*c,++c,++*c,--c;++*c;++c;while(*c){--c;++*c;++c;*c+=7;while(*c)--*c,++c,*c+=6,
    --c;++c;--*c;putchar(*c);--*c;while(*c)--*c,--c,*c+=2,++c;--c;putchar(*c);while(
    *c){--*c;}}*c+=2;--c;while(*c)--*c,++c,--*c,--c;*c+=2;++c;while(*c){--c;--*c;++c
    ;*c+=7;while(*c)--*c,++c,*c+=4,--c;++c;putchar(*c);putchar(*c);while(*c)--*c;--c
    ;}c+=2;}++*c;c-=3;while(*c){--*c;while(*c){--*c;++c;++*c;--c;}++*c;++c;while(*c)
    {--*c;--c;++*c;c+=3;--*c;while(*c)--*c,++c,++*c,--c;*c+=2;++c;while(*c)--*c,--c,
    --*c,++c;c-=3;}c-=4;}c+=2;putchar(*c);c-=3;}getchar();return 0;}/* 10-letters */
    It makes several nonportable assumptions: unsigned default char or two's complement, ASCII, and general reliability of memory alloc. It is, however, written in a small number of simple, easy-to-understand operations, thus increasing its accessibility to beginning programmers.
    Last edited by jafet; 11-08-2006 at 03:31 AM.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  6. #36
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Code:
    ( sizeof(itype) * (CHAR_BIT * 12655ul) / 42039 ((itype) -1 < 0) + 1 )
    12655 decimal digits approx. == 42039 bits. CHAR_BIT converts number of bytes (provided by sizeof) to number of bits, but this could as easily be replaced by 8 on most conventional systems. The last part gets the sign; it doubles the original value if the variable is unsigned, meaning it can store twice a larger range. Salem forgot a multiplication operator after 42039. Thus this finds the number of decimal digits attainable in some integral variable type, assuming internal binary representation.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  7. #37
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    > Salem forgot a multiplication operator after 42039
    Who? Me?

    Quines are pretty mind-bending things
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #38
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    > Result is true if x and y are in [0,1], false otherwise; an obfuscated way of writing this?

    That's right. No, not really an obfuscated way, just a way I first thought may be faster, but it wasn't...

    >
    Code:
    (x == 0 || x == 1) && (y == 0 || y == 1)
    No, that wouldn't work, my x and y are floats!

    > An approximation of pi?

    Yes! An approximation of pi done by simulating a point moving in the complex plane.
    Last edited by TriKri; 11-08-2006 at 03:21 PM.

  9. #39
    Registered User
    Join Date
    Apr 2006
    Posts
    2,017
    Quote Originally Posted by jafet
    The below two programs are based on a common theme.

    Code:
    unsigned i, j;
    for(i = 64; i; --i) {
        for(j = 0; j < 64; ++j)
            putchar("-#"[!(i - 1 & j)]);
        putchar('\n'); }

    This one is completely incomprehensible. The program is summarized in a one-word C-style comment which makes the last line exactly 80 characters long ("10-letters" is a placeholder); you can try guessing it instead

    Code:
    int main(){char*c=calloc(102,1);++c;*c+=8;while(*c){--*c;--c;*c+=4;c+=2;*c+=4;++
    c;++*c;c-=2;}c+=2;*c+=2;++c;*c+=2;c-=4;while(*c){--*c;while(*c){--*c;++c;++*c;--
    c;}++c;while(*c)--*c,++c,putchar(*c),c-=2,++*c,++c;c+=3;while(*c){--*c;while(*c)
    --*c,++c,++*c,--c;++*c;++c;while(*c){--c;++*c;++c;*c+=7;while(*c)--*c,++c,*c+=6,
    --c;++c;--*c;putchar(*c);--*c;while(*c)--*c,--c,*c+=2,++c;--c;putchar(*c);while(
    *c){--*c;}}*c+=2;--c;while(*c)--*c,++c,--*c,--c;*c+=2;++c;while(*c){--c;--*c;++c
    ;*c+=7;while(*c)--*c,++c,*c+=4,--c;++c;putchar(*c);putchar(*c);while(*c)--*c;--c
    ;}c+=2;}++*c;c-=3;while(*c){--*c;while(*c){--*c;++c;++*c;--c;}++*c;++c;while(*c)
    {--*c;--c;++*c;c+=3;--*c;while(*c)--*c,++c,++*c,--c;*c+=2;++c;while(*c)--*c,--c,
    --*c,++c;c-=3;}c-=4;}c+=2;putchar(*c);c-=3;}getchar();return 0;}/* 10-letters */
    It makes several nonportable assumptions: unsigned default char or two's complement, ASCII, and general reliability of memory alloc. It is, however, written in a small number of simple, easy-to-understand operations, thus increasing its accessibility to beginning programmers.
    I don't think many biginning programmers would get these.

    I compiled both on my machine. Very cool. The 10 letter word is Sierpinski. But I still don't get how it's done.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #40
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Quote Originally Posted by jafet
    Code:
    int main(){char*c=calloc(102,1);++c;*c+=8;while(*c){--*c;--c;*c+=4;c+=2;*c+=4;++
    c;++*c;c-=2;}c+=2;*c+=2;++c;*c+=2;c-=4;while(*c){--*c;while(*c){--*c;++c;++*c;--
    c;}++c;while(*c)--*c,++c,putchar(*c),c-=2,++*c,++c;c+=3;while(*c){--*c;while(*c)
    --*c,++c,++*c,--c;++*c;++c;while(*c){--c;++*c;++c;*c+=7;while(*c)--*c,++c,*c+=6,
    --c;++c;--*c;putchar(*c);--*c;while(*c)--*c,--c,*c+=2,++c;--c;putchar(*c);while(
    *c){--*c;}}*c+=2;--c;while(*c)--*c,++c,--*c,--c;*c+=2;++c;while(*c){--c;--*c;++c
    ;*c+=7;while(*c)--*c,++c,*c+=4,--c;++c;putchar(*c);putchar(*c);while(*c)--*c;--c
    ;}c+=2;}++*c;c-=3;while(*c){--*c;while(*c){--*c;++c;++*c;--c;}++*c;++c;while(*c)
    {--*c;--c;++*c;c+=3;--*c;while(*c)--*c,++c,++*c,--c;*c+=2;++c;while(*c)--*c,--c,
    --*c,++c;c-=3;}c-=4;}c+=2;putchar(*c);c-=3;}getchar();return 0;}/* 10-letters */
    Very hard to debug

    Edit: Your first program was very cool, though, I think I'll have to figure out how it works...
    Last edited by TriKri; 11-08-2006 at 03:03 PM.

  11. #41
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The latter looks like compiled-to-C version of a brain........ program.
    Code:
    >++++++++[-<++++>>++++>+<<]>>++>++<<<<[-[->+<]>[->.<<+>>>]>[-[->+<]+>
    [<+>+++++++[->++++++<]>-.-[-<++>]<.[-]]++<[->-<]++>[<->+++++++[->++++<]>..
    [-]<]>>]+<<<[-[->+<]+>[-<+>>>-[->+<]++>[-<-+]<<<]<<<<]>>.<<<]
    A lot more compact. I hope I didn't make any mistakes.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #42
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,403
    Omg!
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  13. #43
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Dear lord! *passes out*
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  14. #44
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Ok, a new one in C++:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main() {
        ofstream fout("x.cpp");
        if (!fout) {
            cout << "Couldn't create \"source.cpp\"" << endl;
            system("pause");
            exit(1);
        }
        string s1 = "#include <iostream>\n#include <fstream>\n#include <string>\nusing namespace std;\n\nint main() {\n    ofstream fout(\"x.cpp\");\n    if (!fout) {\n        cout << \"Couldn't create \\\"source.cpp\\\"\" << endl;\n        system(\"pause\");\n        exit(1);\n    }\n    string s1 = \"\", s2, s;\n    int i;\n    for (i = 0; i < s1.size(); i++) {\n        switch (s1[i]) {\n            case '\\\\':\n                 s2 += \"\\\\\\\\\";\n                 break;\n            case '\\n':\n                 s2 += \"\\\\n\";\n                 break;\n            case '\"':\n                 s2 += \"\\\\\\\"\";\n                 break;\n            default:\n                 s2 += s1[i];\n        }\n    }\n    s = s1.substr(0, 260) + s2 + s1.substr(260, 999);\n    fout << s;\n    fout.close();\n    return 0;\n}\n", s2, s;
        int i;
        for (i = 0; i < s1.size(); i++) {
            switch (s1[i]) {
                case '\\':
                     s2 += "\\\\";
                     break;
                case '\n':
                     s2 += "\\n";
                     break;
                case '"':
                     s2 += "\\\"";
                     break;
                default:
                     s2 += s1[i];
            }
        }
        s = s1.substr(0, 260) + s2 + s1.substr(260, 999);
        fout << s;
        fout.close();
        return 0;
    }
    Maybe not in the most compact form... (^_^);;
    Last edited by TriKri; 11-08-2006 at 04:33 PM.

  15. #45
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Methinks this prints it's own source code, taking into acount // and whatnot. It then writes it out to x.cpp.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

Page 3 of 7 FirstFirst 1234567 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  3. banking2
    By sweet2awy in forum C++ Programming
    Replies: 7
    Last Post: 08-01-2003, 03:01 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21