Python Forum
Create sum clusters of a number sequence
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Create sum clusters of a number sequence
#1
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!
Reply
#2
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.
Reply
#3
(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
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#4
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.
Reply
#5
(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
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#6
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)
buran write Nov-24-2023, 07:43 AM:
Please, don't provide answers to homework questions without effort from OP
BramQBIC likes this post
Reply
#7
(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.
Reply
#8
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??
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Printing k-means clustered clusters ottabe_h 3 3,056 May-19-2021, 09:53 AM
Last Post: piotrkuras
  Printing a number sequence not working properly deepsen 6 3,021 Oct-12-2019, 07:43 PM
Last Post: deepsen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020