Re: Ruby Quiz #62



On Jan 19, 2006, at 8:06 PM, Adam Shelly wrote:

On 1/19/06, James Edward Gray II <james@xxxxxxxxxxxxxxxxxxx> 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



.



Relevant Pages

  • Re: I"m a new Java student and ive got my program working except for one problem help
    ... Enter number 2 if i make an error can some one help Oliver thanks for ... throw new Exception; ... int numberOfNumbers = getNumberOfNumbers; ... sum = sum + getIthNumber; ...
    (comp.lang.java.programmer)
  • Re: I"m a new Java student and ive got my program working except for one problem help
    ... Enter number 2 if i make an error can some one help Oliver thanks for ... throw new Exception; ... int numberOfNumbers = getNumberOfNumbers; ... sum = sum + getIthNumber; ...
    (comp.lang.java.programmer)
  • Re: max(), sum(), next()
    ... The developers choose what they thought would be most useful across the spectrum of programmers and programs after some non-zero amount of debate and discussion. ... In any case, before sum was added as a convenience for summing numbers, *everyone* has to write their own or use reduce. ... Reduce, where S = sum function, raises an exception on empty s. ... part of the reason for adding sum was to eliminate the need to explicitly say 'start my sum at 0' in order to avoid buggy code. ...
    (comp.lang.python)
  • Re: max(), sum(), next()
    ... an exception also (I understand that behaviour is by design, ... You can't use a sum of 0 to indicate when data is missing, ... those cases where summing an empty list is meaningless. ... situations like empty lists rather than have it implicitly ...
    (comp.lang.python)
  • Re: I"m a new Java student and ive got my program working except for one problem help
    ... // Loop until there is no error ... throw new Exception; ... int numberOfNumbers = getNumberOfNumbers; ... sum = sum + getIthNumber; ...
    (comp.lang.java.programmer)