-4

Sorry for being a newbie. But I cant figure out how to generate n integers (might be 0 also) can sum up to some integer K. I can't get through this with Python.

Edit: I want the value of each value limited to some number (say 4) also.

  • generate integers as in series or random? have you explored `range` built-in function – mad_ Oct 02 '18 at 16:02
  • Series or random, both would do. Random is preferred. – bhavesh devjani Oct 02 '18 at 16:04
  • 4
    Please post a code snippet for what you've tried so far, that way someone can give you more specific help than just "How do I x?" – G. Anderson Oct 02 '18 at 16:04
  • @timgeb this would return one value right? – bhavesh devjani Oct 02 '18 at 16:05
  • @mad I'm saying realistically you the most `ints` you are going to have is 20- 1's, and 345 - 0's – vash_the_stampede Oct 02 '18 at 16:06
  • I might have to limit the size of each value to some value (say 2) also – bhavesh devjani Oct 02 '18 at 16:07
  • This would definitely help. use shuffle and slice to the list you need in case of 365 ints.https://stackoverflow.com/questions/976882/shuffling-a-list-of-objects. Example `x=range(1000)` `random.shuffle(x)` `print(x[:365])` – mad_ Oct 02 '18 at 16:09
  • 2
    Here is a trivial solution: `(N-1) *[0] + [K]`. If you want something else, you need to first define the task and only then look for a solution. – zvone Oct 02 '18 at 16:10
  • Figure out a way—any way—that could do it and then implement the procedure in Python. I suspect that's the real problem you're having, has little to do with Python code at this point. – martineau Oct 02 '18 at 16:13
  • Cannot post it as an answer but you might be looking for this `[i for i in list(combinations(range(21),4)) if sum(i)==20]` – mad_ Oct 02 '18 at 17:07

1 Answers1

0

Using random.randint and the condition where a new random is redrawn if sum(l) + r > 20 you can populate of 365 with a sum of 20,

import random

l = []
k = 20
n = 365

for i in range(n):
    if sum(l) > 20:
         l.append(0)
    else:
        r = random.randint(0,2)
        while sum(l) + r > k:
            r = random.randint(0,2)
        l.append(r)

Note

For further randomization of the list I would additionally shuffle it afterwards

random.shuffle(l)

print(sum(l))  # -> 20
print(len(l))  # -> 365
vash_the_stampede
  • 4,590
  • 1
  • 8
  • 20
  • I suspect this could turn into an infinite loop because it may not be possible near the end of the range to get out of the `while` loop. Regardless, the `sum(l)` does not need to be done over and over as part of the `while` loop since nothing within the loop changes the value that it will calculate—so taking it out would at least make it run (potentially a lot) faster. – martineau Oct 02 '18 at 16:39
  • @martineau if we need to maintain a sum less than 20 when a new int is added, isn't it essential that we calculate the sum with newly added random number to ensure that it remains under 20 before adding it and if the sum exceeeds 20 we must select a different random number? I would think that the sum of l + the potential new variable we are going to add is a critical measure – vash_the_stampede Oct 02 '18 at 16:42
  • @martineau could a solution to render this while loop void after sum is 20 to throw in an if else that if sum is 20 or greater then we only append 0's – vash_the_stampede Oct 02 '18 at 16:45
  • @martineau updated with if else, so if sum is greater than 20 while condition is never activated, thoughts? – vash_the_stampede Oct 02 '18 at 16:47
  • vash: Sorry, don't understand your "render this while loop void" comment. As for the `sum()` optimization, your change didn't prevent what I was talking about. What I would suggestion instead (with regard to your original code) would be *before* entering the `while` loop, do `cur_sum = sum(l)`, then change the loop statement itself to `while cur_sum + r > k:`. – martineau Oct 02 '18 at 19:18