Dynamic Programming and US Currency

Have you ever worked at a retail store, or paid close attention to a cashier at a retail store?  If so, you’ve probably seen the process of “making change”.  The problem is, in the US, as follows:

Given some desired amount, X = $D.cc, and the following coin/bill denominations {$100, $50, $20, $10, $5, $1, $0.25, $0.10, $0.05, $0.01}, how can you make X with the least number of bills/coins.

Most people (from the US, anyway) who see this problem will think it is exceptionally easy.  You simply keep giving the customer the highest denomination that’s less than the amount you still owe them.  For example, if you need to make $6.21 you need two bills and three coins ($5, $1, 2 x $0.10, $0.01).  There is no other combination that will yield less than 5 total bills and coins in the US system.

If you’re a programmer, you’ll recognize this ‘algorithm’, as the greedy solution.  With a simple proof, you can see that for US denominations the greedy solution is always optimal.  If you’ve only ever thought about this problem in the real, physical world, you might think that this problem has a greedy optimal algorithm in all currency systems.  But this is not true.  Consider the following example:

How do you make change for $8 using the following denominations {$5, $4, $1}?

A fast-moving cashier using the greedy method would give a $5 and three $1′s, but that’s not optimal.  You can see that in this (strange) currency system it’s better to give two $4′s.  This is a simple example, but you can see how in a poorly conceived currency system change-making could require a lot of thought.  In fact, the only way to give the optimal solution to this problem for any amount in any system is to use Dynamic Programming.  We have the following DP algorithm which solves this problem in all cases:

Given coins denominations d1…dn such that d1 < d2 < … < dn (with d1 = 1, for simplicity), make change for an amount C using the least coins possible.

Let M[j] = optimal number of coins for an amount j

M[j] = min over all i (such that di <= j) of (M[j - di] + 1)

M[0] = 0

The algorithm for solving this program builds up from M[1] to M[C] with each one taking O(n) time to solve.  That leads to an O(n*C) time algorithm.  The greedy algorithm was just O(C), so this DP algorithm is much harder when you have a complicated currency system with many denominations.  If you’re a cashier forced to solve this quickly in your head hundreds of times a day, you don’t have time to run anything but the greedy method!

Before reading this, you might have thought, as I did, that the denominations we use every day were chosen because they are convenient round numbers.  But now I hope that you can see how  currency denominations are a carefully tuned system that yield a greedy solution to common problems.  This is just one cool example of how humans are constantly optimizing algorithms, even when there’s not a computer or computer scientist in sight.

Sizer – A Simple, Useful Chrome Extension

Windows 7 debuted one of my favorite mainstream OS features of all time – Aero snap.  With just a drag one could resize a window to be full or half screen without annoying clicking and dragging.  Recently there have been a wave of OS X apps replicating (and even expanding upon) this functionality such as SizeUp, Spectacle (my personal choice), and Cinch.  However I have now become so dependent on this functionality I find it almost unbearable to use a computer without it.  My workflow has completely adjusted to the side-by-side window style and I can’t go back to dragging my windows to that size manually 5+ times an hour. Continue reading

Opening the Gates to the Tech World

One of the great myths of the startup world is the belief that the recent proliferation of internet technology means that anyone can start a business.  While it is true that in recent years the cost of starting a tech-based business has dropped sharply and many of the barriers to entry have disappeared, there are still many people without the opportunity to get in on the “tech revolution”.  Don’t worry, this is not another article blasting the technology industry for a lack of diversity.  Instead, I want to discuss an idea for a new type of productive philanthropy organization about which I have been thinking for the past few weeks.

Continue reading

Quick Tip: Restore Deleted Google Calendar Events

I use Google Calendar extensively, but I use iCal to view it when I’m at home.  Sometimes the syncing is less-than-ideal, and occasionally iCal will delete some events on the server that I wanted to keep.   I tried a ton of things to fix this, going so far as to get the XML dump of my Google Calendar from the past and finding a way to import it back into iCal.  If you’ve ever had this problem, try Spanning’s Calendar Undelete Tool.  I promise this isn’t some paid ad or spam, this tool just saved my whole calendar so I thought I’d post a shoutout.

Making a Simple, Database-Driven Website with Sinatra and Heroku

When I want to make a website, my instinct is to use Rails.  However, sometimes I don’t need everything (or even 50%) of what Rails provides and it feels like a waste for a quick project.  Sinatra may be the simplest web framework ever, but it is also incredibly powerful.  Combined with Heroku, anyone with a little coding experience can have a website running for free in just a few minutes.

There are plenty of good Sinatra tutorials on the web, however I have found that most of them focus on static content.  For those that don’t, I’ve found that very few will also cover how to integrate Sinatra with Heroku Postgres and get your app up and running in the cloud. After having to do so myself earlier this week, I thought it would be useful to others if I posted a quick and to the point article about how to get Sinatra and ActiveRecord up and running on Heroku.  All of the code that follows is available on my GitHub, right here.  In this tutorial you’ll notice that I bold all filenames and shell commands, this is just for readability and you’re free to ignore it.

Continue reading

The Biggest Problem with WordPress, in One Image

Today I tried to make a new post on this blog, and this is the page with which I was presented:

This page is entirely about writing a post.  Yet the part of the page in which I can compose the post is only 6.12% of the total page area!  The other 93.88% of the page is devoted to blank space,  useless links, website nav, and generally anything but what I need.   This is like writing an essay through the mail slot in my front door.  Am I the only one with this complaint?

Edit:

It appears the text box is resizable, and I am an idiot.  However, I still think it’s bad web design to have it so small by default.  If I didn’t realize, a lot of less technical people definitely won’t.