Page 1 of 1

Splitting an int into a list of subints (addition)

Posted: Fri Feb 05, 2016 12:07 pm
by Kami
So yeah this is not necessarily SP related but I'm trying to create a function like:

Syntax: Select all

split_number(<numbertosplit>, <maxsizeofelements>, <maxnumberofelements>)


The return should be a list of numbers that added together give the original number like:

Code: Select all

10 = ['2', '5', '3']


I've managed to make it to the first two args:

Syntax: Select all

from commands.server import ServerCommand
from random import randint


@ServerCommand('split_number')
def _split_number(command):
list_of_sums = []
number = int(command[1])
max_size = int(command[2])
buffer = number
while buffer > 0:
if buffer >= max_size:
substract = randint(1,max_size)
list_of_sums.append(substract)
buffer = buffer - substract
else:
substract = randint(1, buffer)
list_of_sums.append(substract)
buffer = buffer - substract
print(list_of_sums)


but now I'm wondering, how can I limit the list to only have a given amount of elemnts and still represent the number I want? (the <maxnumberofelements> arg)

Posted: Fri Feb 05, 2016 11:19 pm
by D3CEPTION
you should probably tell us what you are trying to achieve with this

Posted: Fri Feb 05, 2016 11:42 pm
by Ayuto
Funny that you are saying this. :D

You could do something like this:

Syntax: Select all

import random

def split_number(number, max_num_size, max_split_count):
if max_num_size * max_split_count < number:
raise ValueError('Unable to split the number.')

while True:
result = _split_number(number, max_num_size)
if len(result) <= max_split_count:
return result


def _split_number(number, max_num_size):
result = []
rest = number
while rest > 0:
if rest >= max_num_size:
subtrahend = random.randint(1, max_num_size)
else:
subtrahend = random.randint(1, rest)

rest -= subtrahend
result.append(subtrahend)

return result

Posted: Fri Feb 05, 2016 11:57 pm
by D3CEPTION
Ayuto wrote:Funny that you are saying this. :D


are you talking to me?

i am trying to help him, but he already posted the answer in his question, so i was confused what he is actually trying to do

Posted: Sat Feb 06, 2016 12:00 am
by D3CEPTION
btw: i think he is trying to limit the elements ( not the number size ) so you could try sthn like this :

Syntax: Select all

def create_int_list(myint,element_limit):
mylist = []
while sum(mylist) != myint:
mylist = []
takeint = myint

for add in range(1,element_limit+1):
try:
subint = random.randint(1,takeint)
except:
subint = 1
mylist.append(subint)
takeint -= subint
return mylist

Posted: Sat Feb 06, 2016 7:18 am
by Ayuto
D3CEPTION wrote:
Ayuto wrote:Funny that you are saying this. :D


are you talking to me?
Yes, and it's absolutely reasonable to ask that. It's just funny that YOU are asking that, because I have asked you the same question multiple times, when you asked me questions on steam. Moreover, you haven't answered my questions on the other thread. As a result of this it's not fun at all to help you...

Posted: Sat Feb 06, 2016 2:44 pm
by D3CEPTION
Yes, and it's absolutely reasonable to ask that. It's just funny that YOU are asking that, because I have asked you the same question multiple times, when you asked me questions on steam. Moreover, you haven't answered my questions on the other thread. As a result of this it's not fun at all to help you...


to me that is just a basic questions.. but should i have left any questions unanswered, then it probably happened on purpose to not put any unreasonable complexity into an issue where there is none. communication skills are, when you understand what the other persons wants, even though they are using wrong terms/faulty logic/too many infos in their question. therefor, as a responder it can be more helpful to cut an answer into its essence, to help the reader get a more efficient insight into the issue.

Posted: Sat Feb 06, 2016 2:47 pm
by D3CEPTION
although, should it be required from a responder, i am happy to try to answer every single question from now on :)