From: gabriele renzi Date: 2007-07-15T18:05:11+09:00 Subject: Re: xkcd and ugly code On Sun, 15 Jul 2007 17:08:43 +0900, Sharon Phillips wrote: > Hi, > > I read this xkcd strip the other day (http://www.xkcd.com/c287.html) > and, of course, had to solve it. So I whipped up a quick bit of Ruby > code that gave me the answers. Took about 5 minutes and then I was > happy. Except I wasn't, because I looked at what I'd written. Ugly > and not dry at all, sopping wet even; basically six nested loops. > I'll try to reproduce it below. > > Trouble is, I then spend the best part of an afternoon (don't tell my > boss ;-) trying to solve it elegantly, but couldn't. I did come up > with other ways that worked, but they were pretty cumbersome and slow. > > There has to be a 'ruby way' to do this. Any suggestions? not sure about the ruby way, but isn't it a case of the knapsack problem? If so, the greedy and the dynamic programming algorithms for it are quite elegant. -- goto 10: http://www.goto10.it blog it: http://riffraff.blogsome.com blog en: http://www.riffraff.info