Exercise and sample code from Princeton Algorithm course