Splitting an int into a list of subints (addition)
Posted: Fri Feb 05, 2016 12:07 pm
So yeah this is not necessarily SP related but I'm trying to create a function like:
The return should be a list of numbers that added together give the original number like:
I've managed to make it to the first two args:
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)
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)