Fwd: Maximum Sub-Array (#131)

Begin forwarded message:

irb(main):007:0> max_subarray([-10] )
=> []

I would say this is wrong. Despite being a negative sum, the non-empty
subarray [-10] still has the maximum sum.

On Jul 16, 2007, at 9:56 AM, Matthew M. wrote:

irb(main):007:0> max_subarray([-10] )
=> []

I would say this is wrong. Despite being a negative sum, the non-empty
subarray [-10] still has the maximum sum.

That’s what I said too, early in the quiz discussion.

However, it should be noted that the empty Array (considered to have
a sum of zero) is very common in the various versions of this problem
around the net. I’ve been looking a little.

James Edward G. II

On Mon, 16 Jul 2007 23:56:00 +0900, Matthew M. wrote:

irb(main):007:0> max_subarray([-10] )
=> []

I would say this is wrong. Despite being a negative sum, the non-empty
subarray [-10] still has the maximum sum.

I think it makes more sense to choose the zero length array from the
point
of view of gambling or even better investing money in stocks. The way
you’re making the most money in a crisis is to avoid investing
alltogether.

Well, you can do “down speculation” (sorry, don’t know the expression in
english) and get more stocks for the same money but that’s the dual
problem :slight_smile:

All in all, matter of specifications.

On 7/16/07, Matthew M. [email protected] wrote:

irb(main):007:0> max_subarray([-10] )
=> []

I would say this is wrong. Despite being a negative sum, the non-empty
subarray [-10] still has the maximum sum.

It depends on how you want to define the sum of an empty array - zero
or undefined/invalid. I prefer to define sum of an enumerable like
this:

inject(0) {|sum,v| sum+=v}

Or, if you wanted to apply more duck typing, you could define a
generic Zero object that plays well with anything comparable and that
has a unary minus:

inject(Zero) {|sum,v| sum+=v}

where Zero is something like:

Zero = Object.new
class << Zero
include Comparable
def to_s
“Zero”
end
def +(other)
other
end
def -(other)
-other
end
def <=>(other)
-other<=>other
end
end

You could extend this idea to a variety of singleton objects that
mimic basic numbers and can be defined by the other “number” involved
in each operation:

+infinity (i.e. +infinityanthing==+infinity, (+inifinity<=>anything)==1
-infinity
+1 (i.e. 1
thing==thing, 1+thing==thing.succ)
-1 (i.e. (-1)*thing==-thing)
+0 (i.e. anything/+0 == +infinity)
-0

I find +infinity and -infinity singleton objects particularly helpful
especially since there isn’t a portable way to represent these with
built-in objects. I call these singleton objects Max and Min. Many
times these help refactor/DRY my code because I don’t have to treat my
first iteration differently since I can initialize appropriate
variables to Max/Min (+inf/-inf).

On Jul 16, 2007, at 9:02 AM, James Edward G. II wrote:

rubyquiz.com instead?
Sorry, I didn’t notice this message was sent to Ruby T. before it
was sent to me. I didn’t mean to duplicate it.

James Edward G. II