share
Stack OverflowWhat are your programming exercises?
[+130] [31] Bryan Denny
[2008-08-08 19:56:57]
[ exercise ]
[ http://stackoverflow.com/questions/6327/what-are-your-programming-exercises ] [DELETED]

Do you have any programming "exercises" that you do in order to hone your programming skills? Anything from FizzBuzz [1] to more complicated problems to get you thinking about real-life scenarios that you may encounter?

[+127] [2008-08-08 20:00:02] Justin Standard [ACCEPTED]

A lot of great suggestions are archived in this question [1] 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.

Another great technique for practice is to repeat problems using a different language or technology, or a different approach.

[1] http://stackoverflow.com/questions/3088/best-ways-to-teach-a-beginner-to-program
[2] http://codekata.pragprog.com/
[3] http://cymonsgames.retroremakes.com/
[4] http://butunclebob.com/ArticleS.UncleBob.TheBowlingGameKata
[5] http://projecteuler.net/
[6] http://forum.lessthandot.com/viewforum.php?f=102
[7] http://www.pythonchallenge.com/
[8] http://robocode.sourceforge.net/
[9] http://icpcres.ecs.baylor.edu/onlinejudge/
[10] http://felix-halim.net/uva/hunting.php
[11] http://www.karrels.org/Ed/ACM/
[12] http://itasoftware.com/careers/SolveThisWorkHerePuzzles.html
[13] http://www.rubyquiz.com/
[14] http://www.challenge-you.com/
[15] http://hacker.org/

(1) Very interesting list of links! - petr k.
1
[+25] [2008-08-08 19:59:18] Stan
2
[+16] [2008-08-08 23:57:18] Adhip Gupta

Have you tried TopCoder [1]?

[1] http://www.topcoder.com/

3
[+16] [2008-08-11 17:43:17] Shog9

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 [1].

Mavis Beacon Teaches Typing: Oh... Mavis...

[1] http://xkcd.com/323/

4
[+8] [2008-08-11 17:29:41] Brian

Jeff Bay, in the Thoughtworks Anthology [1] 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:

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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().

  6. 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.

  7. 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.

  8. 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.

  9. 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.”

[1] http://www.pragprog.com/titles/twa/thoughtworks-anthology

I have not downvoted, but your answer does not really pertain to the question, does it? - petr k.
(1) It is an exercise, the idea is that for the next 1,000 lines of code you write, place these constraints on your code and see how it helps transform your thinking about OO development. - Brian
(1) Only two instance vars? That's like lisp with only car and cdr! - Joseph Holsten
That is a nice set of rules, I guess I have to try that at some point in time. - queueoverflow
5
[+7] [2009-06-18 10:05:16] Jonathan Maddison

SPOJ [1] - has much of the same ACM problems, but has code submission in a large number of programming languages.

[1] http://www.spoj.pl/

6
[+6] [2008-08-11 18:41:09] basszero

Google code jam [1]

[1] http://code.google.com/codejam/

7
[+5] [2008-08-08 20:00:33] Brian

A series of 20+ code katas are available from the Pragmatic Programmer folks here [1]. 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.

[1] http://codekata.pragprog.com/

8
[+5] [2008-08-08 21:27:52] eviljack

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


For extra credit get #8 to return 82.5, the actual angle between the hour and minute hands at 12:15. - jmucchiello
9
[+5] [2008-08-11 17:41:07] BlaM

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?


10
[+3] [2008-08-08 20:00:22] Jake Pearson

Project Euler [1] is fun.

[1] http://projecteuler.net/

11
[+3] [2008-08-08 20:02:42] SQLMenace

I like Programmer Puzzles [1] on forums, this way you can see other people's clever(or not) answers

[1] http://forum.lessthandot.com/viewforum.php?f=102

12
[+3] [2008-08-08 23:54:01] Chris Jester-Young

I hear that CipherSaber [1] 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 [2] and its related MD4 [3] 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 [4]" 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 [5], again refactoring commonalities (SHA-1 is very similar to MD5).

After that, perhaps SHA-2 [6]? Refactoring your hash system to accommodate the SHA-2 family would be interesting…I'll say no more. :-)

[1] http://ciphersaber.gurus.org/
[2] http://en.wikipedia.org/wiki/MD5
[3] http://en.wikipedia.org/wiki/MD4
[4] http://c2.com/cgi/wiki?OnceAndOnlyOnce
[5] http://en.wikipedia.org/wiki/SHA-1
[6] http://en.wikipedia.org/wiki/SHA-2

13
[+3] [2008-08-11 08:07:37] Tao Zhyn

I once participated in an ACM programming contest.

To practice we went through questions from UVA online-judge [1]. 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 [2] sample [3] questions [4].


Another useful site is Hunting UVA Problems [5]. 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.

[1] http://icpcres.ecs.baylor.edu/onlinejudge/
[2] http://icpcres.ecs.baylor.edu/onlinejudge/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=67
[3] http://icpcres.ecs.baylor.edu/onlinejudge/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=87
[4] http://icpcres.ecs.baylor.edu/onlinejudge/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=53
[5] http://felix-halim.net/uva/hunting.php

14
[+3] [2008-08-11 17:31:55] Adam Lerman

For some puzzles that arent necessarily programming but you may run into at interviews check out http://techinterview.org/


15
[+3] [2009-06-18 10:01:43] Arnis L.

I'm rewriting Tetris [1] game once a 1/2 year. :)

[1] https://github.com/ArnisL/tetris

I do the same thing with my postscript interpreter. There's an old adage that 90% of what you write is crap. So Andy Warhol forced Lou Reed to write one song per day. That way they were sure to come up with 3 good songs a month; even if the songwriting were never to improve!! - luser droog
16
[+2] [2008-08-12 22:12:24] smh

There are some good puzzles at ITA Software [1]. If you do well, they might offer you a job.

[1] http://itasoftware.com/careers/SolveThisWorkHerePuzzles.html

17
[+2] [2008-08-16 03:21:19] quekshuy

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.


18
[+1] [2008-08-12 22:40:19] epatel

How about these challenges?

  1. Write a http server in plain posix
  2. Write a tool to serialize C++ objects
  3. Write a tool to autoswap stuct fields in C

19
[+1] [2008-08-30 02:49:29] Robin Barnes

hacker.org [1] has various ranked challenges.

[1] http://hacker.org

20
[+1] [2009-06-18 09:50:42] Jahanzeb Farooq

The book Programming Interviews Exposed [1] contains a number of good exercises to brush up your programming skills.

[1] http://rads.stackoverflow.com/amzn/click/047012167X

21
[+1] [2011-02-09 15:42:41] Kenny Cason

I found www.hackthissite.org/ [1] to be pretty fun.

They have various challenges including:

  1. Basic and realistic challenges
  2. Programming Missions
  3. Application Missions
  4. Logic Missions

You also learn a lot of useful security related stuff, so it's both fun and useful.

This should keep you busy for a couple of months at least! :)

[1] http://www.hackthissite.org/

22
[+1] [2011-11-21 02:17:36] sprintwatch

Here are the lists of programming exercises [1] for beginners and problem with algorithm [2]

Hope this helpful.

[1] http://www.tesear.com/2011/11/list-of-programming-exercises.html
[2] http://www.tesear.com/2011/11/problem-and-algorithm.html

23
[0] [2008-08-16 02:15:31] jfs

Ruby Quiz [1] has a good set of problems.

[1] http://www.rubyquiz.com/

24
[0] [2008-08-19 07:49:39] steevc

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.


25
[0] [2008-09-16 05:47:34] Jonathan Arkell

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.


26
[0] [2008-09-18 21:20:06] ugasoft

Topcoder and ProjectEuler.

Topcoder is a challenging environment where you can be prized and eventually hired... but the competitors are incredibly strong!


27
[0] [2008-09-26 21:52:26] Bruno Gomes

The Scripting Games [1] 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.

[1] http://www.microsoft.com/technet/scriptcenter/funzone/games/default.mspx

28
[0] [2008-09-27 01:51:54] S.Lott

I wrote all my favorite exercises down in my Building Skills [1] books.

[1] http://homepage.mac.com/s_lott/books/index.html

29
[0] [2009-06-18 09:56:09] Jeffrey Kemp

Find some old software you've written, and expand it so it can send and receive email.


30
[0] [2009-08-07 03:59:18] David Claridge

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).


31