You are invited to Log in or Register a free Frihost Account!

# Coding Challenges.

qebab
Well, there are tons out there. But they are all competitions, and not all are fit for beginning programmers. The idea behind this thread, is to give a few tasks that would be suitable for both experience programmers to test themselves against, and for beginning programmers to try solving and learn under way. This means that any task you think others would enjoy solving, or would learn other something, you should feel free to submit.

What the task is can vary. For instance, it could be a program that calculates the first n prime numbers in less than 1 second, or it could be a program that calculates the first n prime numbers in the least amount of code, or it could be to implement a certain algorithm. We need a format, I propose the following:

Name - Name of the challenge, obviously. Optional.
Difficulty - How hard would you estimate this challenge to be? As in, not how hard for you, but how much knowledge is needed to do it?
Description - A description of the challenge.
Example - Optional. An example of what a certain input to the program should result in. F. ex. 'The first 5 prime numbers are: 2,3,5,7,11. These numbers are only divisible by themselves and 1. Find the n'th prime number.'
Goal - What the goal of writing the program is.
Useful for - What the challenge is useful for (What can you learn by doing it?).
Solution - Optional. If the challenge is, for instance, to find a certain number, give the solution so people can ask what they are doing wrong if they can't get things right.

I don't want solutions to problems in this thread, but if you have to, please, please, please post pseudocode. The idea is, struggeling people can get help, but the only person you challenge is yourself. I think this is a better way to learn than competing against a lot of people that may be a lot better than you, or a lot worse than you. The idea is that we help each others improve. If we make this like a competition, people would be more interested in getting the solutions than working out the solutions themselves. That's not how it is supposed to be.

I wish we could get a sticky on this one, and that people are willing to contribute with problems for both beginners and experienced programmers to test themselves on. I'll try to edit them into this post as we go along, so discussion about them can go on later in the thread. I'll start by submitting a couple:

Name - FizzBuzz (Yeah, I stole that one).
Difficulty - Easy, even for the beginner.
Description - Make a program that takes an input n. For every number between (including endpoints) 1 and n, prints Fizz if the number is divisible by 3, Buzz if it is divisible by 5, FizzBuzz if divisible by both, otherwise it prints that number. Every output should end with a newline ("\n").
Example - Giving your program the number 8, should result in the following on your screen:
1
2
Fizz
4
Buzz
Fizz
7
8
Hint - Modulo or integer division and then multiplication.
Goals - Realise a couple of things about string treatment / number treatment, 'golf' your code (see how short you can get it), find different ways to do the same thing.
Solution - No Solution.

Name - PrimeFibs.
Difficulty - Easy.
Description - Make a program that calculates the sum of the first n fibonacci numbers that are also prime. The n'th number in the fibonacci row is defined as the sum of the (n-1)th number and the (n-2)th number.
Example - The first 6 fibonacci numbers are: 1,2,3,5,8,13.
A prime is a number that is divisible only by itself and 1. 2,3,5 and 13 are primes. The sum of the first 6 fibonacci numbers, non-prime numbers excluded, is 23.
Hint - How many of the prime numbers are even?
Goals - The goals of this challenge is to make efficient code. You should NOT have to wait for a very long time to get the program to print out answers. There are a lot of ways to save time with both the fibonacci row and primes. Use sources if you can not figure them out by yourself.
Solution - No numeric solution, but giving the program n = 6, should return in 23. The next fibonacci prime is 89.

Name - Bot.
Difficulty - Medium, some knowledge about the IRC protocol and sockets is needed.
Description - Make an IRC bot in another language than mIRC. It should be able to come online in channel, and stay there, as well as react to certain messages (for instance greet people when they enter the channel).
No examples.
Hint - IRC protocol.
Goals - To learn about sockets, the IRC protocol, and to build a framework for expanding a bot.
No solution.

Name - HTML interpreter.
Difficulty - Not that difficult, but requires a lot of time and a good deal of knowledge about HTML standards and interpreting.
Description - Make a program that will interpret a HTML file, and print it with correct formatting to your screen. You make this challenge as hard as you want it, whether or not you want to interpret CSS and comments... entirely up to you.
Example - The following HTML: <html><head><title>HTML interpreter.</title></head><body><p>We're toying with html here.</p><br><br><p><strong>I THINK WE MIGHT BE LUNATICS.</strong></p></body></html>
Should print:
Title: HTML interpreter.
We're toying with html here.

I THINK WE MIGHT BE LUNACTICS.
to your screen (unless I've forgotten my HTML. Haven't used it in years, really).
Goals - Learning text processing (regexps, for instance) and file handling.
No solution.

Name - The N queens problem (Yeah, I stole this one too).
Difficulty - Above medium, unless you have studied programming or maths on university level.
Description - We have a N*N chess board with exactly N queens on it. We want to position our queens so that no queen may threaten another. A queen can move diagonally, up and down as far as she wishes. For instance, consider this 4*4 board:
e e e e
e e Q e
e e e e
e e e e
Where e means empty and Q means Queen. The Queen threatens every cell in her row and coloumn, as well as the two diagonals ((2,4),(4,2),(4,4),(2,2),(1,1)). Hence you can't place another on those places.
Your job is to write a program that finds the number of distinct, and the number of unique solutions. The distinct solutions are mirrors and rotations of each other, when you eliminate all mirrors and rotations, the number you are left with is the number of unique solutions.
No examples.
Hint: How do you most easily generate boards, how do you most easily test them? For N = 8, if you generate every possible board, your program will take too long.
Goals - Getting better at abstract problemsolving and thinking. Learn how to break a large problem into smaller and more manable parts.
Solution: For N = 8, there are 92 distinct and 12 unique solutions (Yes, I solved this one ).

So does anyone else wish to contribute? Post in the thread, or send me a pm and I will edit it to include your challenge as soon as possible.
qebab
Oh, and is it too much to ask for a sticky?