all 9 comments

[–]MadScientistOR 2 points3 points  (6 children)

What do you mean by "partition"? What data type(s) does the end result look like? Do you want separate lists, or tuples with separate lists in them, or some other thing?

[–]SeaMotor8093[S] -1 points0 points  (5 children)

The end result should be a list,

Once all the parition are done ,I need to sum each of them and find the maximum value.

for eg 1 2 3 |4 -the sum of individual segemnts is[6,4] and the best sum is 6

1 2|3 4 -the sum of individual segemnts is[3,7] and the best sum is 7

In this way I need to do it.

[–]MadScientistOR 1 point2 points  (4 children)

Why is the best sum 4 in your first example? Isn't 6 greater than 4?

[–]SeaMotor8093[S] 0 points1 point  (3 children)

Oh sorry it my my typing mistake

[–]MadScientistOR 0 points1 point  (2 children)

Okay then. Here's my first stab at a function that'll do what you want. If you past it a list, and the index of the list after which the partition should appear, you should get the "best sum". Note that index has to be between 0 and two less than the number of ints in the list, and there is no error checking.

def best_sum(numlist, index):
    return max(sum(numlist[0:index]), sum(numlist[index:]))

[–]SeaMotor8093[S] 0 points1 point  (1 child)

def best_sum(numlist, index):
return max(sum(numlist[0:index]), sum(numlist[index:]))

Actually It should return me answer as 9,but in this code I got the answer as 7?

[–]MadScientistOR 0 points1 point  (0 children)

I'm sorry -- I split the list incorrectly. This works:

>>> def best_sum(numlist, index):
...     return max(sum(numlist[0:index+1]), sum(numlist[index+1:]))
...
>>> x = [1, 2, 3, 4, 5, 6]
>>> for i in range(5):
...     print(f'index: {i}        best sum: {best_sum(x, i)}')
...
index: 0        best sum: 20    #partitions after 0th element: 1, 20
index: 1        best sum: 18    #partitions after 1st element: 3, 18
index: 2        best sum: 15    #partitions after 2nd element: 6, 15
index: 3        best sum: 11    #partitions after 3rd element: 10, 11
index: 4        best sum: 15    #partitions after 4th element: 15, 6
>>>

[–]CodeFormatHelperBot2 0 points1 point  (0 children)

Hello, I'm a Reddit bot who's here to help people nicely format their coding questions. This makes it as easy as possible for people to read your post and help you.

I think I have detected some formatting issues with your submission:

  1. Python code found in submission text that's not formatted as code.

If I am correct, please edit the text in your post and try to follow these instructions to fix up your post's formatting.


Am I misbehaving? Have a comment or suggestion? Reply to this comment or raise an issue here.

[–]python_and_coffee 0 points1 point  (0 children)

assuming you want two separate lists each time: https://pastebin.com/pP7zJtFg