3

I have this following code where I am trying to form an array 'opt'. Here, I am taking three possible values of 'pos_set' = [1, 2, 3] and in a similar manner, I can extend this. But, I just want a generalized code for any possible integer value of pos_set.

    opt = []
    if pos_set == 1:
        for j in range(1, n):
            opt.append([j])
    elif pos_set == 2:
        for j in range(1, n):
            for k in range(j+1, n):
                opt.append([j, k])
    elif pos_set == 3:
        for j in range(1, n):
            for k in range(j+1, n):
                for l in range(k+1, n):
                    opt.append([j, k, l])

For more clarity, I am doing this with the objective of collecting all possibilities if you roll an n-sided die and keep doing this as long as you keep rolling larger values.

For instance, if you roll a sequence 1-2-6-4, in this case after getting a 4 following a larger no. 6, you stop rolling. Similarly, if you roll a sequence 1-2-6-6, in this case you get a repeated 6 so you stop because it is not larger than your previous roll. I am considering cases before the smaller or same number occurs i.e., [1, 2, 6] in both cases.

If you guys can help me, I would be grateful.

2
  • [[1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [2, 3], [2, 4], [2, 5], [2, 6], [3, 4], [3, 5], [3, 6], [4, 5], [4, 6], [5, 6]] is this right output for pos_set =2 ? Commented Sep 2, 2018 at 6:55
  • Yes, you got that right. :) Commented Sep 2, 2018 at 6:58

3 Answers 3

2

You can use the following recursive function:

def f(p, n, i=1):
    if p == 0:
        return [[]]
    return [[j, *l] for j in range(i, n) for l in f(p - 1, n, j + 1)]

so that:

print(f(1, 7))
print(f(2, 7))
print(f(3, 7))

outputs:

[[1], [2], [3], [4], [5], [6]]
[[1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [2, 3], [2, 4], [2, 5], [2, 6], [3, 4], [3, 5], [3, 6], [4, 5], [4, 6], [5, 6]]
[[1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 2, 6], [1, 3, 4], [1, 3, 5], [1, 3, 6], [1, 4, 5], [1, 4, 6], [1, 5, 6], [2, 3, 4], [2, 3, 5], [2, 3, 6], [2, 4, 5], [2, 4, 6], [2, 5, 6], [3, 4, 5], [3, 4, 6], [3, 5, 6], [4, 5, 6]]
Sign up to request clarification or add additional context in comments.

1 Comment

Recursion is not my strong suit. Thanks, buddy! :)
1

Was not sure by how you went about your question were you just concerned for constructing your qpos_set, or did you need help with the code to produce the actual output that will meet your objective.

Here is a code I wrote that will roll n- sided die (I just set it at 100 for demonstration) but this will continue to roll another die and append its value until that number is equal or less and then the previous roll.

I'm going to hold off on breaking down the code unless you needed this portion as well. Let me know!

import random

die = list(range(1, 100))
temp = [0, 1, 2]
winners = []

while temp[1] > temp[0]:
    temp[0] = random.randint(1, len(die))
    temp[1] = random.randint(1, len(die))
    temp[0] = temp[2]
    if temp[1] > temp[0]:
        winners.append(temp[1])
        temp[2] = temp[1]
    else:
        winners.append(temp[1])
        break

print(winners)

2 Comments

Thanks for the effort, but I only needed the generalized function. Although, if you are interested, you can attempt the main problem which is "if you roll an n-sided die and keep doing this as long as you keep rolling larger values: find the expected values of both the sum of 'winner' and the number of rolls used to get it. For n=6, sum is 9 and no. of rolls is 3 if [1, 2, 6] are the rolls value."
@Shubh definately, its 4am here just got back from the bar haha I'll give it a shot tomorrow sounds fun
1

There is a built-in solution for this, itertools.combinations:

In [5]: import itertools
In [6]: list(itertools.combinations(range(1,7), 3))
Out[6]:
[(1, 2, 3),
 (1, 2, 4),
 (1, 2, 5),
 (1, 2, 6),
 (1, 3, 4),
 (1, 3, 5),
 (1, 3, 6),
 (1, 4, 5),
 (1, 4, 6),
 (1, 5, 6),
 (2, 3, 4),
 (2, 3, 5),
 (2, 3, 6),
 (2, 4, 5),
 (2, 4, 6),
 (2, 5, 6),
 (3, 4, 5),
 (3, 4, 6),
 (3, 5, 6),
 (4, 5, 6)]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.