 Programming Challenges

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! 2. Registered User
Join Date
Jul 2003
Posts
28
swap two integers without using a temporary variable.

3. 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:
Last edited by Sepero; 08-19-2003 at 05:05 PM.

4. Really bored person
Join Date
May 2003
Posts
145
Solution attached.

5. 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 08:17 PM.

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

- Suramya

7. Registered User
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. Bad Speller ^^
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);
}
}```

9. 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;
}

10. Registered User
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') 11. Registered User
Join Date
Jul 2003
Posts
28
Compute the complex product ( a + ib ) * ( c + id ) using only three real multiplications

12. Registered User
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.

 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.

13. Sepero:

Code:
```int main(int argc, char **argv)
{
char buf;

if (argc != 2) return 1;
if (argv != '-') return 1;

if (argv == "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 14. Registered User
Join Date
Apr 2001
Location
SF Bay Area, CA
Posts
14,947
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. Registered User
Join Date
Mar 2001
Posts
729
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 12: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
•