# Programming Challenges

Show 40 post(s) from this thread on one page
Page 1 of 19 1234511 ... Last
• 08-19-2003, 10:59 AM
trashthing
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! :D
• 08-19-2003, 04:18 PM
swap two integers without using a temporary variable.
• 08-19-2003, 05:57 PM
Sepero
Cool. That was fun. How 'bout another. :p

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

Edit:
• 08-19-2003, 07:59 PM
cfaun5
Solution attached.
• 08-19-2003, 09:13 PM
dchidelf
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.
• 08-19-2003, 09:39 PM
Suramya
Quote:

swap two integers without using a temporary variable.
This thread is a cool idea. Here is my solution.

- Suramya
• 08-19-2003, 10:55 PM
bwkaz
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:

Quote:

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... ;)
• 08-20-2003, 02:53 AM
tecknophreak
prolly not. :D

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);         } }```
• 08-20-2003, 04:07 AM
o0zi
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
Welcome to my web server!
This web server was written in Perl.
</BODY></HTML>';
\$HTTPclient->close;
}
• 08-20-2003, 08:10 AM
x_Ray
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') :)
• 08-20-2003, 10:52 AM
Compute the complex product ( a + ib ) * ( c + id ) using only three real multiplications
• 08-20-2003, 11:10 AM
joelc
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.

 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.
• 08-20-2003, 12:08 PM
sploo22
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 :p
• 08-20-2003, 09:18 PM
bwkaz
Quote:

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... ;)
• 08-21-2003, 01:42 AM
Strogian
Quote: