From: James Edward Gray II Date: 2006-01-20T13:12:43+09:00 Subject: Re: Ruby Quiz #62 On Jan 19, 2006, at 8:06 PM, Adam Shelly wrote: > On 1/19/06, James Edward Gray II wrote: >> This version of the code seems to fix at least this case: > > There's a small bug in your code, James: > If you pass a box that fits the trunk exactly, you get an exception. > The culprit is this line from #try_adding box > >> # score the remaining open space >> score = rows[1..-2].map { |row| row[/_(_+)/, 1] }.compact. >> map { |open| open.size }.inject { |sum, n| >> sum + n } > > score is nil if there is no open space. you need to make it ... > inject(0) {|sum,n| ... Great catch again. I'm glad someone is watching over me. I need all the help I can get, as we can see! > You've got a good heuristic, but it still makes mistakes. Oh, no doubt. Good example. > On a totally unrelated note - I discovered something odd when working > on this solution: > irb(main):010:0> r=1..9 > => 1..9 > irb(main):011:0> r.last > => 9 > irb(main):012:0> r.include? r.last > => true > irb(main):013:0> r=1...9 > => 1...9 > irb(main):014:0> r.last > => 9 > irb(main):015:0> r.include? r.last > => false > > I really expected (1...9).last to return 8. I guess this is just > another way ranges are screwy. Na, that's not odd at all. Just tell yourself .. is inclusive and ... is exclusive (with regards to the last member). See how that extra dot pushes it off the end there... ;) James Edward Gray II