Overflow error in Python Code for Maximum Subarray Using Recursion -


my program seems not work code. i'm new python i'm not sure if language related error. i'm using python 2.7.8.

    = [-1, -2, 3, 4, -5, 6]      def main():         a,b,c = find_maximum_subarray_recursive(a)                                                            print a,b,c      def find_maximum_crossing_subarray(a, low, mid,  high):          #mid = math.floor((low + high)//2)         left_sum = float("-inf")         sum = 0         = mid         max_left = 0         in range (mid, low, -1):             sum = sum + a[i]             if sum > left_sum:                  left_sum = sum                  max_left =          right_sum = float("-inf")         sum = 0         j = mid + 1         max_right = 0         j in range (mid + 1, high):             sum = sum + a[j]             if sum > right_sum:                  right_sum = sum                  max_right = j         return (max_left, max_right, left_sum + right_sum)       def find_maximum_subarray_recursive(a, low = 0, high = -1):         high = len(a)         if high == low:             return (low, high, a[low])         else:             mid = math.floor((low + high) // 2)             left_low, left_high, left_sum = find_maximum_subarray_recursive(a, low, mid)             right_low, right_high, right_sum = find_maximum_subarray_recursive(a, mid + 1, high)             cross_low, cross_high, cross_sum = find_maximum_crossing_subarray(a, low, mid,  high)             if left_sum >= right_sum & left_sum >= cross_sum:                 return (left_low, left_high, left_sum)             elif right_sum >= left_sum & right_sum >= cross_sum:                 return (right_low, right_high, right_sum)             else:                 return (cross_low, cross_high, cross_sum)      if __name__ == '__main__':main() 

the function find_maximum_crossing_subarray(a, low, mid, high) working fine life of me can't seem find error function find_maximum_subarray_recursive(a, low, high). causing program overflow. don't understand if there problem logic or syntax. appreciate if please explain me. many thanks!

def find_maximum_subarray_recursive(a, low = 0, high = -1):     high = len(a) 

this high = len(a) line looks logic error me. i'm guessing original reasoning was, "if user doesn't supply value high parameter, we'll supply him highest index a can accept". if that's intention, there's 2 problems:

  1. you set value regardless of whether or not user supplied value himself.
  2. the highest index a can accept len(a)-1, not len(a).

ignoring user's supplied value of high cause of infinite loop. example, find_maximum_subarray_recursive(a, 0, 6) calculates mid of 3 , calls find_maximum_subarray_recursive(a, 0, 3) in order find left_sum. 3 value thrown away , replaced 6. next function calculates mid of 3 , calls find_maximum_subarray_recursive(a, 0, 3)... , on, forever , ever.

so think beginning of function should more like:

def find_maximum_subarray_recursive(a, low = 0, high = -1):     if high == -1: high = len(a)-1 

and thing:

    else:         mid = math.floor((low + high) // 2) 

this looks type error me. math.floor returns floating point value, mid should integer (since you're going use index list in future, , lists accept integers). don't need math.floor call @ all. // operator returns integer. think line should more like:

mid = (low + high) // 2 

these changes don't entirely fix program. you'll result of 2 3 7, 7 isn't maximum subarray sum a. largest sum 8, subarray [3, 4, -5, 6].

but @ least you're not overflowing more!


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -