At the York Code Dojo this week, the challenge was to create a small program that output the childrens nursery rhyme “There was an old lady who swallowed a fly…”
Myself and my partner Mahy approached it using C++.
Our solution works by making an array of commonly used words and splicing them together using some rather convoluted code! I think it could still go a little smaller, but it does get hard to tweak the more ‘optimal’ you make it. There are some repeated words that could probably save a bit more.
The final size of the program is somewhere below 700 bytes.
A few simple tricks:
The macro saves characters, and completes the previous line
There is in inner loop for the repeated verse which selects pairs of animals
initializing the ints globaly sets them to zero (something I’d forgotten)
The point of this exercise is to get you to think outside the box, and gain a deeper understanding of the limitations & features of the language you choose. There’s a theory that says our perception of the world is based on the internal language we use to describe it. Certainly, it is true for programming languages; they each have their quirks.