Splitting an int into a list of subints (addition)

Please post any questions about developing your plugin here. Please use the search function before posting!
User avatar
Kami
Global Moderator
Posts: 264
Joined: Wed Aug 15, 2012 1:24 am
Location: Germany

Splitting an int into a list of subints (addition)

Postby Kami » Fri Feb 05, 2016 12:07 pm

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)
User avatar
D3CEPTION
Senior Member
Posts: 129
Joined: Tue Jan 26, 2016 1:24 pm
Location: Switzerland

Postby D3CEPTION » Fri Feb 05, 2016 11:19 pm

you should probably tell us what you are trying to achieve with this
User avatar
Ayuto
Project Leader
Posts: 2209
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Postby Ayuto » Fri Feb 05, 2016 11:42 pm

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
User avatar
D3CEPTION
Senior Member
Posts: 129
Joined: Tue Jan 26, 2016 1:24 pm
Location: Switzerland

Postby D3CEPTION » Fri Feb 05, 2016 11:57 pm

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
User avatar
D3CEPTION
Senior Member
Posts: 129
Joined: Tue Jan 26, 2016 1:24 pm
Location: Switzerland

Postby D3CEPTION » Sat Feb 06, 2016 12:00 am

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
User avatar
Ayuto
Project Leader
Posts: 2209
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Postby Ayuto » Sat Feb 06, 2016 7:18 am

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...
User avatar
D3CEPTION
Senior Member
Posts: 129
Joined: Tue Jan 26, 2016 1:24 pm
Location: Switzerland

Postby D3CEPTION » Sat Feb 06, 2016 2:44 pm

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.
User avatar
D3CEPTION
Senior Member
Posts: 129
Joined: Tue Jan 26, 2016 1:24 pm
Location: Switzerland

Postby D3CEPTION » Sat Feb 06, 2016 2:47 pm

although, should it be required from a responder, i am happy to try to answer every single question from now on :)

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 71 guests