Do you have any programming "exercises" that you do in order to hone your programming skills? Anything from FizzBuzz to more complicated problems to get you thinking about real-life scenarios that you may encounter?
A lot of great suggestions are archived in this question about teaching beginners. Particularly the bits about what projects you should try for beginners.
Here is a summary of some resources out there for programmer practice.
- Code Katas from the Pragmatic Programmer.
- Cymonsgames - game/coding exercises
- Bowling Game Kata to practice TDD
- Project Euler - mathematical / computational problems.
- Less Than Dot - a forum with progamming puzzles.
- The Python Challenge - a series of riddles/puzzles that can be solved w/ python.
- Robocode - practice by designing a battle robot in Java
- UVA Online Judge - used in ACM programming contests. Along with UVA Hunting.
- Ed's Programming Contest - sample problems
- ITA Software Hiring Puzzles
- Ruby Quiz - set of problems in ruby.
- challenge-you.com - built on the google app engine.
- hacker.org challenges
Another great technique for practice is to repeat problems using a different language or technology, or a different approach.
A series of 20+ code katas are available from the Pragmatic Programmer folks here. I particular like the Supermarket Pricing challenge ($3.00 each or 2 for $5.00) kind of stuff. As Dave says:
How do you get to be a great musician? It helps to know the theory, and to understand the mechanics of your instrument. It helps to have talent. But ultimately, greatness comes practicing; applying the theory over and over again, using feedback to get better every time.
I like Programmer Puzzles on forums, this way you can see other people's clever(or not) answers
Ok enough! I mean, what's next?
"What is your favorite color?" Or maybe "What does you desktop background look like?"
Com'on people. I'm not really picking on this question, but this site has seen a slew of "fluff" questions. Are people gaming the system?
some general questions that I found fun:
1) Reversing a string in place -- using no swap (this is prb a C question)
2) reverse a linked list (iteratively)
3) reverse a linked list (recursively)
4) converting strings of numbers to ints (character by character, don't cheat and use a class library)
5) splitting a string of strings into an array of strings
6) converting milliseconds into hours, minutes,seconds, and remaining milliseconds
7) join a 2D array of strings into one string
8) given a function:
float getAngle(int hours, int minutes) - return the angle between them if the hours and minutes were represented on a regular clock. Example getAngle(12, 15) would return 90.0
I hear that CipherSaber is a good programming exercise. Try implementing that in a variety of languages! (My picks, for the sake of choosing extremes in the abstraction continuum, would include Scheme and assembly language.)
I've recently taken to implementing MD5 and its related MD4 functions. The skill is not in coding it as such (although it's always nice to see that the test vectors pass):
- How to factor out the commonalities between MD4 and MD5.
- How to write each of the algorithms in more elegant ways, in your chosen language.
- Most implementations of MD5 I see use 64 lines of code across the 4 rounds—this is good for speed, but is not very elegant.
- There are more "OAOO" approaches to this, if you're using a higher-order functions.
- Trying different languages, and exploring the abstraction mechanisms they provide to allow for such elegance.
Once you run out of languages to try, you can then try to implement SHA-1, again refactoring commonalities (SHA-1 is very similar to MD5).
After that, perhaps SHA-2? Refactoring your hash system to accommodate the SHA-2 family would be interesting…I'll say no more. :-)
I once participated in an ACM programming contest.
To practice we went through questions from UVA online-judge. It provides a huge list of problems and allows you to submit your answers to see if they are correct. I do believe it supports C, C++ and Java submissions. The questions deal with graphing problems, number crunching, simulations, etc. Half the battle is trying to figure out what type of question it is.
Here are a few sample questions.
Another useful site is Hunting UVA Problems. It will create a list of problems that are roughly the next easiest for you. You will need to sign up to UVA online-judge before using this tool.
Jeff Bay, in the Thoughtworks Anthology has a nice set of 'rules' (object calisthenics) to follow to get your code back to a more object oriented state. It's an exercise to help change the way you think about structuring your code, and give you better OO habits when writing real-world code. The ideas is to right 1,000 lines of code while imposing the following constraints:
Use only one level of indentation per method. If you need more than one level, you need to create a second method and call it from the first. This is one of the most important constraints in the exercise.
Don’t use the ‘else’ keyword. Test for a condition with an if-statement and exit the routine if it’s not met. This prevents if-else chaining; and every routine does just one thing. You’re getting the idea.
Wrap all primitives and strings. This directly addresses “primitive obsession.” If you want to use an integer, you first have to create a class (even an inner class) to identify it’s true role. So zip codes are an object not an integer, for example. This makes for far clearer and more testable code.
Use only one dot per line. This step prevents you from reaching deeply into other objects to get at fields or methods, and thereby conceptually breaking encapsulation.
Don’t abbreviate names. This constraint avoids the procedural verbosity that is created by certain forms of redundancy—if you have to type the full name of a method or variable, you’re likely to spend more time thinking about its name. And you’ll avoid having objects called Order with methods entitled shipOrder(). Instead, your code will have more calls such as Order.ship().
Keep entities small. This means no more than 50 lines per class and no more than 10 classes per package. The 50 lines per class constraint is crucial. Not only does it force concision and keep classes focused, but it means most classes can fit on a single screen in any editor/IDE.
Don’t use any classes with more than two instance variables. This is perhaps the hardest constraint. Bay’s point is that with more than two instance variables, there is almost certainly a reason to subgroup some variables into a separate class.
Use first-class collections. In other words, any class that contains a collection should contain no other member variables. The idea is an extension of primitive obsession. If you need a class that’s a subsumes the collection, then write it that way.
Don’t use setters, getters, or properties. This is a radical approach to enforcing encapsulation. It also requires implementation of dependency injection approaches and adherence to the maxim “tell, don’t ask.”
For some puzzles that arent necessarily programming but you may run into at interviews check out http://techinterview.org/
I'm a programmer and I have several web projects. I don't need external excercises to keep me busy. If I feel that I should do something I just take one of my projects and improve it:
- Add a Spam-Filter that works
- Improve performance if it is not as fast as I'd like it to be
- Optimize algorithms to best fit my user's behavior, trying to hold them longer, clicking more ads ;) ...
- Add some more automation to anything
Really, I don't need to write another sort algorithm when there are so many interesting "real world" problems to solve.
You should try that and be creative yourselves. Maybe you are the first to develop the 100% spam protection mechanism?
Pushups: Gets the heart pumpin', the mind workin', and wears out those wrists encouraging you to keep the code concise and avoid redundant comments.
Darts: Strengthens your wrists, so you don't become crippled after too many push-ups / redundant comments. Plus, you can (and should) drink while playing, allowing you to hit that elusive Ballmer Peak.
Mavis Beacon Teaches Typing: Oh... Mavis...
There are some good puzzles at ITA Software. If you do well, they might offer you a job.
How about these challenges?
- Write a http server in plain posix
- Write a tool to serialize C++ objects
- Write a tool to autoswap stuct fields in C
Haven't seen this mentioned: http://www.challenge-you.com/
It's built on the Google App Engine. I like the part where it gives a breakdown of the submissions by language.
My standard exercise when learning a new language has been to write a Mandelbrot set app. Done it in BBC Basic, Turbo Pascal and, recently, C# among others. The TP version was the most optimised with all sorts of approximations going on, but then it was running on a 12MHz 286 or something.
I worked through some of the Python Challenge a few years back. That was fun.
It depends on what kind of skills you want to work on...
Project Euler is good for building math and problem solving skills. If you are a web developer, building your own webserver will teach you a LOT about lowlevel HTTP, which can't be a bad thing. Choosing a language and implementing it is a great way to learn all about the deeper aspects of programming. A subset of scheme is fairly easy to implement, or, you could even implement one of the wierder languages like brainfuck for befunge. Implementing tools for programming is another excellent exercise. Editor plugins are pretty easy to do, and a good start.
Topcoder and ProjectEuler.
Topcoder is a challenging environment where you can be prized and eventually hired... but the competitors are incredibly strong!
The Scripting Games by Microsoft are held every year. You can also look at past years' problems and solutions and exercise your Perl, VBScript and Windows Powershell skills.
The book Programming Interviews Exposed contains a number of good exercises to brush up your programming skills.
Find some old software you've written, and expand it so it can send and receive email.
SPOJ - has much of the same ACM problems, but has code submission in a large number of programming languages.
One thing I like to do with my college friends when we want to practise optimisation, or more commonly when we want to learn a new language, is we write programs to generate primes (usually using eratosthenes sieve or some variant thereof). Whoever has generated the largest prime by the end of the night wins! While it seems a fairly simple algorithm, there are a lot of small optimisations you can make (especially in languages a bit closer to the machine, like C). If it's a scripting language we play prime number golf (writing the smallest program we can that outputs primes).