Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generalising different for-loop method in Python

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.

like image 376
Shubh Avatar asked Mar 26 '26 05:03

Shubh


2 Answers

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]]
like image 155
blhsing Avatar answered Mar 28 '26 02:03

blhsing


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)
like image 30
vash_the_stampede Avatar answered Mar 28 '26 02:03

vash_the_stampede