3
$\begingroup$

You want to print vectors with n elements, where:

  • the first element can have the values: e1.1, e1.2, e1.2;

  • the second element can assume the values: e2.1, e2.2, e2.3;

  • ...;

  • ...;

  • the nth element can assume the values: en.1, en.2, en.3.

I should print all possible vectors; eg:

{e1.1, e2.1, en.1 ...};

{e1.2, e2.1, en.1 ...};

etc. etc.

Someone could indicate an algorithm making use of while/for loop to print these vectors?

Thanks so much!

| cite | improve this question | | | | |
$\endgroup$
  • 1
    $\begingroup$ Those are not permutations. A permutation of a set S is a bijective function from S onto itself. What did you try? $\endgroup$ – quicksort Dec 27 '16 at 20:57
1
$\begingroup$

The idea is to think of your output as representing a number in a mixed base. The $i$th digit has base $b_i$, where $b_i$ is the number of options for the $i$th element. You can then print all possible vectors essentially by implementing a counter. I'll let you work out the details.

| cite | improve this answer | | | | |
$\endgroup$
0
$\begingroup$

Here's a python program that does this for $n=4$. In the $i$th iteration, each vector of length $i$ is appended with one of three possible strings to create vectors of length $i+1$.

n = 4
res = [["e1.1"], ["e1.2"], ["e1.3"]]
for i in range(2, n+1):
    temp = res[:]   #make a copy of current list
    res = []
    for x in temp:
        res.append(x + ["e" + str(i) + ".1"])
        res.append(x + ["e" + str(i) + ".2"])
        res.append(x + ["e" + str(i) + ".3"])

print(res)        
| cite | improve this answer | | | | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.