Python Forum

Full Version: Create sum clusters of a number sequence
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi All,

I want to write a code for the following:

1. form clusters of adjacent digits whose sum is <= 8
2. The order of the numbers may not be changed
3. The clusters should be designed in such a way that the sum should be as close to 8 as possible
4. As few clusters as possible should be designed

An example of the input: 11111111113143111
An example of one of the possible outputs: (1+1+1+1+1+1+1+1)+(1+1+3+1)+(4+3)+(1+1+1) = 8+7+7+3
An other example of one of the possible outputs: (1+1+1+1+1)+(1+1+1+1+1)+(3+1+4)(3+1+1+1) = 5+5+8+6
As output I want all possibilities.

Thanks for the help!
Seems a bit vague and boring. Is there a point or a data analysis application for this? Detecting alien intelligence in signals reaching the Earth??

I like Python because it can be used to solve practical problems and save time!

1. 2. 3. and 4. seem to contradict each other.
(Nov-23-2023, 06:52 AM)Pedroski55 Wrote: [ -> ]Seems a bit vague and boring
Vague yes, but it is a divertimento away from the daily chores.
Next to the remarks above, there should be a caveat not to introduce a nuber > 8 in the series.
And the first solution is not ok, because the 4 + 3 could be 4 + 3 + 1 if I understand the task.
Always eager to learn, I have a solution of 11 lines. Anything shorter?
Paul
Thx for looking at it Shy

@Pedroski55 hopefully I explained better Blush
@DPaul al examples are ok because it is one of the possibilities.

Sorry for my vague description. I am only discovering Python through GIS applications (QGIS).
This is about designing a fiber optic network, this question is a small part of it.

The number series represents adjacent houses with the number of demand points in each building. That is why the order of the numbers must not change. Starting from a POC (small distribution box), only a maximum of 8 demand points can be served.
I would like to look for all possible options and then use the variance formula to find the best proposal because the clusters should be as well balanced as possible.
Hopefully I have been a bit clearer now, feel free to ask if you need any more information.
(Nov-22-2023, 01:54 PM)BramQBIC Wrote: [ -> ]I want to write a code for the following:
Bram,
I think forum rules require you to post the code you have tried.
We can't post code in homework for "free".
Paul
Still not quite sure exactly what you want, but maybe this will give you some ideas.

import re
numstring = '11111111113143111'
numlist = [int(numstring[i]) for i in range(len(numstring))]
total = sum(numlist)
print(f"The sum of the numbers is {total}")
if total % 8 == 0:
    print("We can make {sum(numlist)/8} groups with a sum total of 8 from the numbers.")

def count8(alist):    
    group = []
    for i in range(len(alist)):
        group.append(alist[i])
        if sum(group) > 8:
            del group[-1]            
        elif sum(group) == 8:
            return group

groups = []
while not sum(numlist) <= 8:
    mygroup = count8(numlist)
    print(mygroup)
    groups.append(mygroup)
    for m in mygroup:
        numstring2 = re.sub(str(m), 'X', numstring, count=1)
        numstring = numstring2
    numstring = numstring.replace('X', '')    
    numlist = [int(numstring[i]) for i in range(len(numstring))]
groups.append(numlist)
(Nov-23-2023, 04:10 PM)DPaul Wrote: [ -> ]
(Nov-22-2023, 01:54 PM)BramQBIC Wrote: [ -> ]I want to write a code for the following:
Bram,
I think forum rules require you to post the code you have tried.
We can't post code in homework for "free".
Paul

Paul,
Ok, understood, next time will follow forum rules.
Thinking some more about this, I came up with the ideas below, but I don't know if that is similar to what you are looking for??

Maybe this will give you some ideas on an approach. I was intrigued!

import re
import random

# make a random numlist
# numlist = [random.randint(1, 8) for i in range(151)]
numlist = [random.randint(1, 4) for i in range(51)]
total = sum(numlist)
print(f"The sum of the numbers is {total}")
if total % 8 == 0:
    print(f"We can make maximum {sum(numlist)/8} groups with a sum total of 8 from the numbers.")
else:
    print(f"We can make maximum {int(sum(numlist)/8)} groups with a sum total of 8 and 1 smaller group from the numbers.")

def count8(alist):
    num = 0
    for i in range(len(alist)-1):
        num = num + alist[i]
        print('sum =', num)
        if num + alist[i+1] > 8:
            return (i, num, alist[0:i+1])

groups = []
while sum(numlist) > 8:
    group = count8(numlist)
    print('group:', group)
    groups.append(group[2])
    numlist = numlist[group[0]+1:]

for g in groups:
    print(g)
But I am still not sure if that is what you are looking for??