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
#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


Messages In This Thread
RE: Create sum clusters of a number sequence - by Pedroski55 - Nov-24-2023, 12:09 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Printing k-means clustered clusters ottabe_h 3 3,191 May-19-2021, 09:53 AM
Last Post: piotrkuras
  Printing a number sequence not working properly deepsen 6 3,243 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