Programming Challenges


Page 1 of 19 1234511 ... LastLast
Results 1 to 15 of 278

Thread: Programming Challenges

  1. #1

    Programming Challenges

    hi. i have started this thread for people to post programming challenges for others to solve. the programming challenges are exercises for people to practice programming. once you have completed a challenge post your code. when you are posting your code, please quote the challenge that you are responding to.

    happy coding!
    i was once trapped by windows, but linux set me free...

  2. #2
    Join Date
    Jul 2003
    Posts
    28
    swap two integers without using a temporary variable.

  3. #3
    Join Date
    Mar 2003
    Location
    Tampa, FL USA
    Posts
    2,193
    Cool. That was fun. How 'bout another.

    Written in C++. I deleted my previous post and included the file instead. This way it doesn't spoil the fun for others.

    Edit:
    This was in reply to grady's post
    Attached Files Attached Files
    Last edited by Sepero; 08-19-2003 at 06:05 PM.

  4. #4
    Join Date
    May 2003
    Posts
    145
    Solution attached.
    Attached Files Attached Files

  5. #5
    Join Date
    Jan 2001
    Location
    Somewhere in middle America
    Posts
    164
    What if I'm allergic to carrots?

    Hmmm...lets see...the hard way.
    Code:
    #include <stdio.h>
    
    #define flip_bit if(a&1){                  \
         if(b&1) { a >>= 1; a |= 0x80000000; } \
         else    { a >>= 1; a &= 0x7FFFFFFF; } \
         b >>= 1; b |= 0x80000000;             \
      }                                        \
      else {                                   \
         if(b&1) { a >>= 1; a |= 0x80000000; } \
         else    { a >>= 1; a &= 0x7FFFFFFF; } \
         b >>=1; b &= 0x7FFFFFFF;              \
      }
    
    int main(){
      int a = -343;
      int b = 711;
    
      fprintf(stdout,"a: %d  b: %d\n",a,b);
    
      flip_bit flip_bit flip_bit flip_bit
      flip_bit flip_bit flip_bit flip_bit
      flip_bit flip_bit flip_bit flip_bit
      flip_bit flip_bit flip_bit flip_bit
      flip_bit flip_bit flip_bit flip_bit
      flip_bit flip_bit flip_bit flip_bit
      flip_bit flip_bit flip_bit flip_bit
      flip_bit flip_bit flip_bit flip_bit
    
      fprintf(stdout,"a: %d  b: %d\n",a,b);
    
      return 0;
    }
    This isn't really a legitimate solution, so I figured there would be no harm in posting it in the message.
    Last edited by dchidelf; 08-19-2003 at 09:17 PM.

  6. #6
    Join Date
    Mar 2003
    Location
    Earth [I think...]
    Posts
    414
    Originally posted by grady
    swap two integers without using a temporary variable.
    This thread is a cool idea. Here is my solution.

    - Suramya
    Attached Files Attached Files
    --------------------------------------------------
    My Website: http://www.suramya.com
    My Blog: http://www.suramya.com/blog
    Registered Linux User #: 309391
    --------------------------------------------------

    *************************************************
    Disclaimer:
    Any errors in spelling, tact, or fact are transmission errors.
    *************************************************

  7. #7
    Join Date
    Apr 2001
    Location
    SF Bay Area, CA
    Posts
    14,947
    Here's another challenge. Write a Web server.

    Oh, wait, that's way too hard.

    Better:

    Write a rot13 implementation, those'd be cool to see. Take stdin, transform it according to rot13 (changing nothing if the symbol isn't a letter, but rotating both uppercase and lowercase letters, separately), and print it to stdout. This program makes a nifty tool when people start talking in threads like this:

    Ubj vf rirelbar gbqnl?

    Bu, abg onq, ohg V jbaqre vs nalobql ryfr trgf guvf guernq?

    V'z fher gurl qba'g, ohg bu jryy.
    Of course, I doubt anybody would do that anymore (that was something that happened more in the old G:OT forum), but still...

  8. #8
    Join Date
    May 2001
    Location
    Uh, I'm somewhere where I don't know where I am.
    Posts
    1,228
    prolly not.

    Code:
    #include <iostream>
    
    using std::cin;
    using std::cout;
    
    int main() {
    	char ch;	
    	while (cin.get(ch)) {
    		cout << (char)(isalpha(ch) ? ((isupper(ch) ? 'A': 'a') 
    				+ ((ch - (isupper(ch) ? 'A' : 'a') + 13) % 26))
    				: ch);
    	}
    }
    if (i_forgot && this_is_about_code)
    language = c++;

  9. #9
    Join Date
    Aug 2002
    Location
    Essex, UK
    Posts
    937
    Here's my Perl solution to swapping two variables:

    ($a, $b) = ($b, $a);
    :D

    Web server did you say? Hmm...

    #!/usr/bin/perl -w
    use HTTP::Daemon;
    $HTTPserver=HTTP::Daemon->new(Timeout =>600);
    print "My url is: ",$HTTPserver->url,".\n";
    while ($HTTPclient = $HTTPserver->accept) {
    $HTTPclient->autoflush(1);
    print $HTTPclient
    '<HTML><HEAD><TITLE>
    Welcome to my web server!
    </TITLE></HEAD><BODY>
    This web server was written in Perl.
    </BODY></HTML>';
    $HTTPclient->close;
    }

  10. #10
    Join Date
    Apr 2002
    Posts
    259
    rot13 in python

    Code:
    #!/usr/bin/python
    
    def rot13(text):
        new_text = []
        for x in text[0:]:
            up = False
            if x.isupper():
                x = x.lower()
                up = True
            byte = ord(x)
            if byte >= ord('a') and byte <= ord('z'):
                x = chr((byte - ord('a') + 13) % 26 + ord('a'))
                if up:
                    x = x.upper()
            new_text.append(x)
        return ''.join(new_text)
    
    text = raw_input('Enter some text: ')
    print rot13(text)
    Or the easy way; text.encode('rot13')
    OggVorbis gets mobile...
    http://www.gentoo.org
    Is that a penquin in your pocket or are you just happy to see me?

  11. #11
    Join Date
    Jul 2003
    Posts
    28
    Compute the complex product ( a + ib ) * ( c + id ) using only three real multiplications

  12. #12
    Join Date
    May 2003
    Location
    WI
    Posts
    75
    Hmm, my school had an upper level CS course called Applied Algorithms. The course basically consisted of a list of 30 specific programs handed out at the beginning of the semester, of which you had to write any 20 of them before the end. Class time would be spent clarifying rules, discussing implementation problems, advice, etc. I never took the course, but I am good friends with the professor. I might be able to get and post these programs when I visit the school again in a couple of weeks, if anyone is interested.

    [edit] Several, but not all, of these programs had very simple implementations (less than a page in c++), but had speed requirments (terms of Bigo) that make them more complicated.
    I'm really f'lar. Just at work and forgot my password for the moment.

  13. #13
    Join Date
    Aug 2002
    Location
    Cayman Islands
    Posts
    681
    Sepero:

    Code:
    int main(int argc, char **argv)
    {
        char buf[100];
    
        if (argc != 2) return 1;
        if (argv[1][0] != '-') return 1;
    
        if (argv[1][1] == "c")
        {
            fgets(buf, 100, stdin);
            printf("x\n");
            return 0;
        } else {
            fgets(buf, 100, stdin);
            printf("She sells $ea sheLLs, by tHe se@ shorE.\n");
            return 0;
        } 
    
        return 1;
    }
    program -c to compress, program -d to decompress.

    I love cheating
    Scotty: Captain, we din' can reference it!
    Kirk: Analysis, Mr. Spock?
    Spock: Captain, it doesn't appear in the symbol table.
    Kirk: Then it's of external origin?
    Spock: Affirmative.
    Kirk: Mr. Sulu, go to pass two.
    Sulu: Aye aye, sir, going to pass two.

  14. #14
    Join Date
    Apr 2001
    Location
    SF Bay Area, CA
    Posts
    14,947
    Originally posted by grady
    Compute the complex product ( a + ib ) * ( c + id ) using only three real multiplications
    ( a + ib ) * ( c + id ) = ac - bd + i ( ad + bc )

    So, I don't think I can figure a way to do it in less than four multiplications...

    Hmm, maybe this:

    ( a + ib ) c + ( a + ib ) id

    But that's not a canonical-form complex number, not really...

    I can't come up with a way to relate ad + bc and ac - bd, either... if I could, that might lower the number of multiplications needed...

    Good question! Just wish I could figure an answer...

  15. #15
    Join Date
    Mar 2001
    Posts
    729
    Originally posted by grady
    Compute the complex product ( a + ib ) * ( c + id ) using only three real multiplications
    What's a real multiplication? Can I use division?

    EDIT: Oh, I'm close. I know it.

    EDIT: Ah nevermind, I got nothing.
    Last edited by Strogian; 08-21-2003 at 01:48 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •