What we need is a 9-cent coin

Lately I’ve been thinking about coins. They may be going out of style around the world, but where I am at least they are still a fact of daily life. After fumbling around with coins at the cashier trying to find the right change, I’ve often wondered whether we can improve a currency by changing the denomination of the coins.

For example, we encounter prices ending in .99 more often than, say, .37. Wouldn’t it make sense to have a 99-cent coin? How would one even go about trying to answer this question?

It took a long train ride with no Internet connection for me to sit down and start hacking out an answer. Other people have tackled it before (pdf), but there were some aspects that I felt were still left unexplored.

I’ve put my thoughts and explorations online at this website. In the process, I’ve come to a few conclusions:

  • The coins of the US dollar are not really rationally designed, but they are not bad when it comes to making change, in the trade off between the number of different types of coins, and the average number of coins needed to make change.
  • The optimal denomination that uses three coins is 1, 9, 30. This is when the real-life distribution of prices is taken into account.

I hope that you enjoy reading my analysis as much as I have had in working out the problem.

Changer-ranger – Fun with the optimal change problem


The most time-wasting command-line problem to diagnose …

If I had to vote for the computer problem that consumes the most time in diagnosis vs. the ease of actually fixing it, I would definitely choose the line termination issue.

It’s the simplest thing to understand, in principle. Different operating systems have different conventions in how they format their files, in particular plain text files. If you open up a plain text file in a editor application like Notepad on Windows, you’ll see the content as simple text characters without any formatting. There are also “invisible” characters, such as tab and “newline”, which affect the formatting of the text but aren’t really text themselves.

Windows uses two invisible characters, called CR (Carriage Return) and LF (Line Feed) to represent each new line. This dates back to the days when computers didn’t have monitors, but instead literally printed their output onto spools of paper. CR+LF were instructions to the printer to bring the printer head back to the origin, and feed the paper forward, in order to begin printing a new line of text.

Unix/Linux and Mac OS X have a different convention. They only use one character, LF, to represent new lines. (Even more confusingly, the classic Mac OS uses only CR). Therefore, when you have a text file from a Windows system on your Linux system, you’ll first have to convert the line endings first, or weird things will happen, and at first you might have no idea what is going on because newline characters are invisible, so the file will look completely normal when you open it in a text editor. Instead you’ll blame yourself, like the 99% of the time when things don’t work, because that’s usually some other fault in your code, like a typo in your elaborate regular expression.

Fortunately it’s happened to me frequently enough before that when things don’t work as they should on the command line, one of the first things I do is to check the file type. In Linux this is easily done with:

file FILENAME.txt

You should expect to get something like:


But if you see this:

FILENAME.txt: ASCII text, with CRLF line terminators

Then it’s probably coming from a Windows system and needs to be converted before you work with it on Linux/Unix/Mac OS X.

It’s easy to fix if you have the dos2unix utility that’s bundled on many Linux systems.

dos2unix FILENAME.txt # Silently overwrite original
dos2unix -n FILENAME.txt NEWFILE.txt # Write to new file, keep original

The reverse can be done with unix2dos.

If you don’t have dos2unix, you can use sed. The escape character \r represents CR, so the following simply means “remove the CR character from each line”.

sed -i 's/\r//' FILENAME.txt # Overwrite original
sed 's/\r//' FILENAME.txt > NEWFILE.txt # Write to new file, keep original

Further reading here and on the manual page of dos2unix.

The R tidyverse

Most regular R users will have felt the influence of Hadley Wickham, whether through the widely-used ggplot2 package that implements the “grammar of graphics”, devtools, plyr, … the list goes on. I was astounded when I first realized that the same person was responsible for all these really useful things.

Most software packages aim at providing tools to make particular tasks easier in a certain language. In comparison, many of the tools that he has developed are in effect streamlining the grammar of the language itself. Once you use ggplot2 and see how intuitive it is to deal with statistical graphics in that way, then the base R plot commands feel impossibly clunky. Similarly, his paper on tidy data and the accompanying tidyr and plyr packages articulate basic ideas about data should be organized in tables. These are ideas that sound very simple, and most of us have probably had some similar thoughts cross our minds as we struggled to reshape raw data into analyzable form, but I certainly would not have been able to formulate the concepts so clearly or implement solutions to change our relationship to data wrangling.

The various packages have seemed to evolve towards a common style and design philosophy, and late last year most of them have been bundled together in a ‘super-package’ called tidyverse. It makes installation much easier, because now you can make sure all these inter-dependent packages are up-to-date with a single command, and probably makes development easier for him and his team. It also goes together with a book titled R for Data Science that he and a coauthor have just released, which is also available online. Noted here for future reference!

3D vision with the Pulfrich Effect

The Pulfrich Effect is an optical phenomenon where objects (or images) moving in a single plane can appear to be in 3D when the light reaching one eye is dimmed, e.g. with a filter. It also has a curious history – Carl Pulfrich (biography – pdf), who discovered the phenomenon, was blind in one eye and never observed it for himself, but nonetheless made many contributions to stereoscopy (the study of 3D vision) in both theory and the construction of apparatus.

Unlike other forms of stereoscopy, this only works with moving objects or animations; it does not work with still images! But what’s really cool is that you don’t need any special equipment to view it, beyond a piece of darkened glass or plastic to act as a filter. Videos exhibiting the Pulfrich effect can be viewed on a normal monitor or TV screen.

I’ve made my own Javascript animations as demos for the Pulfrich effect (posted as GitHub Gists and rendered by bl.ocks.org):

Screen Shot 2017-01-10 at 00.57.42.png

Screenshot from my animated explanation of the Pulfrich Effect

Continue reading

Visualize metagenomes in a web browser

In my day job I work with metagenomes from animals and protists that have bacterial symbionts, and I’ve blogged here before about why visualizations are so useful to metagenomics (mostly to flog my own R package). However most existing tools, including my own, require that you install additional software and all the libraries that come with them, and also be familiar with the command line. That’s pretty standard these days for anyone who wants to do serious work with such data, but it can be a big hurdle for teaching. Time in the classroom is limited, and ideally we want to spend more time teaching biology than debugging package installation in R.

I’ve therefore written up a simple browser-based visualization for rendering coverage-GC% plots, called gbtlite. There’s no need to mess around with data structures in R, or worrying about how to install required packages for your operating system. The visualization uses the D3.js Javascript library, which is popular for web applications. If you’ve played with infographics on the New York Times website, then you’ve probably seen something built with D3.

Continue reading

Hosting a website with GitHub Pages

My website seaheuchin.info is a labor of love for me – it is a work of family history and biography that I researched and started to write up in 2007, but only some months ago did I publish it online as a website.


Web-publishing is the poor(er) man’s self-publishing, but it does offer some advantages over paper. I can easily update the site with new features, such as photographs and transcriptions of original documents, and also incorporate interactive features like maps and timelines. It’s perfect for a serial procrastinator like me, because I can make something that is mostly done but not yet perfect available as a working version. For example, I’m still working on text boxes to explain the historical background for something mentioned in the main text of the biography – many of them are already up, but for some I have only an outline of what I would like to write.

In this blog post I’ll explain what free tools (“free” in the sense of not paying money for it, not the “free” in Free Software Foundation) I’ve used to build and host the website. I want to show how a hobbyist with modest web skills, like me, can still get things online quickly and painlessly.

Continue reading

Blast from the past – C-Dogs

When I was a kid back in the late 90s and early 00s I spent a LOT of time on these computer games: SimCity 2000, TetraBlocks (a Tetris clone), and C-Dogs.

C-Dogs was something special – a freeware MS-DOS-based game where blocky cartoon figures ran around a 2D map shooting things up. That sounds pretty primitive, but there were some great things about it: the graphics/artwork were awesome classics of the pixel-art genre, you could customize your characters and build custom maps, and the EXPLOSIONS. The best way to combine all that was great about this game was to build a custom map with lots of barrels of explosives lying around, throw in some grenades, and watch the whole map blow up. Very satisfying.

Screen Shot 2016-11-24 at 23.28.27.png

Exploding fuel barrel

Continue reading