Guess the code!

This is a discussion on Guess the code! within the C++ Programming forums, part of the General Programming Boards category; Isn't that one of the programming challenges on this board?...

  1. #46
    Darkness Prevails Dark_Phoenix's Avatar
    Join Date
    Oct 2006
    Location
    Houston, Texas
    Posts
    174
    Isn't that one of the programming challenges on this board?
    Using Code::Blocks and Windows XP

    In every hero, there COULD be a villain!

  2. #47
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Quote Originally Posted by manutd
    Methinks this prints it's own source code, taking into acount // and whatnot. It then writes it out to x.cpp.
    Right! d-(^_^)-b

    > Isn't that one of the programming challenges on this board?

    Really? ö I didn't know!
    Last edited by TriKri; 11-08-2006 at 04:42 PM.

  3. #48
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Indeed it is, Dark_Pheonix. Sorry about the Shakespearean speak, BTW, I have a play coming up, enow.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  4. #49
    Registered User
    Join Date
    Apr 2006
    Posts
    2,011
    Here's a another. Same deal, write without recursion, thereby making apparent what it does.
    Code:
    template<typename T, typename Iterator>
    T whatDoIDo2(T (*func)(T, T), Iterator begin, Iterator end)
    {
        return begin+1 != end ? func(*begin, whatDoIDo2( func, begin+1)) : *begin;
    }
    This one may be easier, because I got rid of the second condition. I also changed the end condition and made it into a template, both in compliance with C++ common practice. As such, Itterator is an iterator of type T object in a STL compatible container. Begin is the starting point, end is the beyond the end point.

    Both of my questions have very simmilar answers.
    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.

  5. #50
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Quote Originally Posted by King Mir
    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;
    }
    Well, I got that if you called the function with parameters (f, 'x', "abcd"), the function would return character f( f( f( f( 'x', 'a' ), 'b' ), 'c' ), 'd' ), right? When can you benefit that function? (o_O)

  6. #51
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by jafet
    Code:
    ( sizeof(itype) * (CHAR_BIT * 12655ul) / 42039 ((itype) -1 < 0) + 1 )
    Salem forgot a multiplication operator after 42039.
    No -- I misposted it, but I'd already posted a correction (it wasn't missing a multiply) and an explanation.
    [edit=2]But I really take it as a compliment that you confused li'l ol' me with Salem.

    [edit]
    Quote Originally Posted by TriKri
    > 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!
    Faster than...?
    Code:
    x >= 0 && x <= 1 && y >= 0 && y <= 1
    The short-circuit operation of the && versus the branching makes for an interesting comparison in the speed category.
    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.*

  7. #52
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    > The short-circuit operation of the && versus the branching makes for an interesting comparison in the speed category.
    Yes, that's about what I realized too.

  8. #53
    Registered User
    Join Date
    Apr 2006
    Posts
    2,011
    Quote Originally Posted by TriKri
    Well, I got that if you called the function with parameters (f, 'x', "abcd"), the function would return character f( f( f( f( 'x', 'a' ), 'b' ), 'c' ), 'd' ), right? When can you benefit that function? (o_O)
    So write that as a loop.

    Here's an example of a call to that function (this should make it easy):
    Code:
    char sum(a,b){
       return a+b)
    }
    
    ...
    
    whatDoIDo(sum,0,charArray)
    In retrospect I should have done what I did with the other function, using an end itteraotor and either a template or just int.
    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.

  9. #54
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    But I really take it as a compliment that you confused li'l ol' me with Salem.
    Busy thinking about code...


    Equivalent to King Mir's:
    Code:
    std::accumulate(begin + 1, end, *begin, some_binary_function);

    Nobody cracked my first three yet?
    Last edited by jafet; 11-09-2006 at 02:10 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;}

  10. #55
    Registered User
    Join Date
    Apr 2006
    Posts
    2,011
    yep.
    That's the second exactly. (expect for the functor part)

    The first is very very simmilar.
    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.

  11. #56
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    The first is
    Code:
    std::accumulate(xs, xs+strlen(xs), a, func);
    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. #57
    Registered User
    Join Date
    Apr 2006
    Posts
    2,011
    yep.

    I got both of these are from Haskell -- the foldl and foldr1 functions. In haskell, there are no loops, so you have to use recursion to repeat anything. There are also no variables (except constants, which are really 0 arguement functions), so you can't store a result for later use. This is mainly because there is no such thing as later; there are no sequence points in Haskell.

    foldl, foldr, foldl1, and foldr1 are all functions used to apply a binary function to a list and return the final result.
    Last edited by King Mir; 11-09-2006 at 08:27 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.

  13. #58
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    >
    Code:
    main(a){while(a=getchar())putchar(a-1/((a^32&a)/13*2-11)*13);}
    (a^32&a) makes all lower case letters a uppe case letters
    'A' first char = 5 when divided by 13
    'A' – 'M' /13 gives 5
    'N' – 'Z' /13 gives 6
    prev*2 gives 10 and 12 respectively
    prev-11 gives -1 and +1 respectively
    (other values for a will give less that -1 or more than +1)
    1/prev gives -1 and 1 respectively, non-letters a will give 0
    a - prev*13 will cause both upper and lower case letters 'A' to 'M' to jump to letters 'N' to 'Z' and vice versa; no non-letter characters will be affected.

    The program prints the line you writes but the two halves of the alphabet has flipped.
    Good work, you must have studied the ascii table for a while, huh? (b^_^)

    Code:
    main(a){while(a=getchar())putchar(a-1/(~(~a|32)/13*2-11)*13);}
    main(a){while(a=getchar())putchar(a+1/(11-(a&32^a)/13*2)*13);}
    These programs do the same thing.
    Last edited by TriKri; 11-09-2006 at 02:57 PM.

  14. #59
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by jafet
    Nobody cracked my first three yet?
    As already mentioned, ROT-13.

    I took the original intent of this thread to be posting bits that were useful but possibly not well-recognized snippets, as opposed to obfuscated code.

    Along that line,
    Code:
    int foo(const void *a, const void *b)
    {
       const int *x = a, *y = b;
       return (*x > *y) - (*x < *y);
    }
    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.*

  15. #60
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Yes, that's what I understood. In other words, the code should hve an obscure purpose, not implementation.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

Page 4 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