15 Programming Exercises
November 11, 2007
Being what could be charitably described as a programming language junkie, over the past few years I’ve amassed a number of programming exercises aimed towards establishing fluency with a new language; some of these are barely beyond Hello World or FizzBuzz, while some can get quite tricky. Mackenzie Morgan expressed interest in improving her Python-fu; after listing half-a-dozen of these ideas, I said “Hey, maybe this would make a good blog post.” As such, I present to you a list of programming challenges. Many of these have been culled from various sources on the Internet, and I have endeavoured to provide credit where due.
- In general, each language treats collections differently - some (Nu and Smalltalk) start indexing from 1, whereas most start indexing from 0; Ruby returns nil when one tries to access an element outside of an array index, whereas most others throw some sort of an exception; each language has its own set of ideals that it deems best. Your challenge is to take your favorite language and turn this paradigm on its head - if you use a relaxed, permissive language such as Ruby, write a NeuroticArray class that panics at the slightest opportunity; if you use a stricter language such as Java or Haskell, write a RelaxedArray structure that throws as few errors as possible. Bonus points will be awarded if the public interface to your new collection is nearly identical to that of the original type.
- Write a text munger, as detailed in this Ruby Quiz link. The most obvious solution to this problem is to remove the head and tail characters of each word, shuffle the remaining letters, and replace the head and tail characters; as such, this teaches character manipulation, sequence shuffling, and string concatenation.
- Implement a collection-backed regular expression matcher, as detailed in this Ruby Quiz. This teaches about object-orientation (strictly speaking, OOP is not necessary for this exercise), regular expressions, and increases awareness of the limitations of languages (Ruby’s built-in limit on regular expression length inspired this quiz).
- Write a simple script that obfuscates English text by adding any possible accent or umlaut to each letter in a sentence. This teaches the basics of regular expressions and associative arrays.
- Write a high-level input library, as detailed in this Ruby quiz. Not only does it teach how to write a good API, but it also forces you to consider the various needs of the people who use your API’s. Can you satisfy everybody, or will you be forever mediocre?
- Write a tool to turn Hacker Keys into English-readable strings. This is an excellent situation in which to learn about the power (and limitations) of regular expressions, and also shows programmers about the dangers of constantly-changing specifications. Bonus points will be awarded if your solution reads in the hacker key specification from configuration files; even more will be awarded if you write a tool that parses user input into a hacker key dump.
- Write a parser for HQ9+ or HQ9++. These exceedingly simple esoteric languages are very useful for learning the basics of regular expression usage, and implementing an REPL is always challenging and interesting.
- Build your own Scheme interpreter. Scheme’s syntax is extremely minimal, as such, with a good parsing toolkit - please, if you value your sanity, do not try to write a parser using nothing but regular expressions - writing a Scheme interpreter is far, far easier than you’d think. The fantastic Haskell tutorial Write Yourself a Scheme in 48 Hours is an excellent introduction to a language that is, at times, terrifying. (In a conversation with my mom about the ambitious scope of this particular exercise as applied to Haskell, I said “Imagine, Mom, if immediately after learning how to say ‘Hello’ in Japanese, the second assignment was to write a Russian-Japanese dictionary.”)
- Write an ID3v1 tag parser, as detailed in this Ruby Quiz. The ID3v1 format is extremely simple, and a parser can be written easily and understandably in even a very low-level language. This will teach about file input and output, and also present the difficulties associated with programs that deal robustly with malformed input.
- Write a graphical frontend to a system tool such as fortune. In lightweight scripting languages this task will be much easier than with heavy-duty languages such as Objective-C or Java; however, no matter what langauge you choose this will teach you about construction of a simple GUI and how to interface with the operating system. Bonus points will be awarded if you write a cross-platform tool that invokes the correct tool for a particular task no matter its operating systme - e.g. a tool that invokes the tracert tool on Windows and the traceroute command on *nix.
- Most programming languages have support for more than one GUI toolkit; write a program that compares the widget support for as many different toolkits as possible. Not only will this illustrate the sheer magnitude of the difference between a good and a bad API, but programming a GUI correctly provides a very visceral satisfaction. The difficulties of building a project that links into multiple heavy-duty libraries may play a part in this project, depending on the language used. You could also contrast GUI builders with writing the widget-placement code manually in this project.
- Finish the Ruby Quiz (yes, I do realize that half of these ideas are links to the Ruby Quiz, now will you shut up?) metakoans.rb exercise. If you can’t finish it - I sure couldn’t, on my first try at least - look at the solutions that other people submitted and try to understand them. If you’re feeling brave, implement the quiz and the solution in your language of choice. If you’re feeling like a goddamn hero, solve the continuation koans.
- While programming in any language, one will build up an often-used set of useful functions and modifications; clean up, comment, and publish these extensions. You never know who might find them useful!
- Take a simple program you’ve written - for example, one that prints out the lyrics to the 12 Days of Christmas - and remove all familiar concepts from it. If you’re working in C, don’t use a for loop. If you’re using an imperative language, recurse rather than iterate. If you’re using a language with mutable state, program in a purely functional style.
- Solve all the Project Euler problems that you can - and make sure that you pick the right algorithm for the job. There is a world of difference between what you learn when you write a brute-force solution that takes the whole night to run and a complicated but effective solution that takes 10 seconds to run.
Have any more ideas? Did I leave out your pet exercise? Leave a comment.
Entry Filed under: code. .
6 Comments Add your own
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed
1.
Victoria | November 11, 2007 at 11:12 pm
Ooo, thanks for sharing! I’m bookmarking this for later as I seriously need to do more hardcore technical programming, even in my primary languages.
Very cool that you know Mackenzie btw - I know her from the DevChix mailing list and it just totally makes sense that you prodigies would all be friends.
2.
Tom | December 1, 2007 at 9:21 pm
I don’t for a second believe that you’re a college freshman and that you know that much about programming. ‘fess up.
3.
Gopi | January 18, 2008 at 12:25 pm
1
121
12321
1234321
4.
Jenga! | March 14, 2008 at 1:31 pm
Thus, is poo.
5.
Pbur’s Adventures &&hellip | May 14, 2008 at 4:36 am
[...] 15 Programming Exercises (tags: programming language exercise) Leave a Comment [...]
6.
rascunho » Blog Arc&hellip | June 30, 2008 at 8:52 pm
[...] 15 Programming Exercises « Important Shock As such, I present to you a list of programming challenges. (tags: importantshock.wordpress.com 2008 mes5 dia30 at_tecp programming tests Python code_kata ***** ruby tutorial) [...]