"Fnord" or "The evil empire cheerfuly striked back at the merry-colored pretty princess"

One of my favorite exercises for young programmers is the following (widely known program):

Write a program that will read lists of words from the following files:

  • verbs.txt – contains verb, the others are respectively -
  • nouns.txt,
  • adverbs.txt,
  • and adjectives.txt.

After reading the words, the program will construct sentences of the form:

The <adj> <noun> <adv> <verb> the <adj> <adj> <noun>.

You could of course allow for different sentence structures, or more types of words (prepositions). This site made me remember that. This exercise is really handy, and it also allows for great variations of difficulty (at least in C). If you tell the programmer that he or she must support variable number of words, or just a maximum, or how to define the files… the options are endless. The interesting thing about this exercise, is that in C, it is much more effort to write then in Python (with all the built-in modules). This makes the exercise even more effective: After letting the new programmer painfully implement it in C, I let the guy (or girl) implement it in Python. Really fun to watch. Especially after he or she discovers the meaning of ‘one-liner’ :)

This entry was posted in C, Programming, Programming Philosophy, Python, Teaching Programming and tagged , . Bookmark the permalink.