python sequence to generate combination of 4 numbers from 3 digits

Question:

Could any share the python script to generate combination of 4 numbers from 3 digits for e.g 1, 5, and 8 . 1 digit can be repeated but it should have all 3 digits it should be displayed in the following format for example

1 1 5 8
1 5 5 8
1 5 8 8
....
def GetCombFrom(arr):
for i in range(4):
    for j in range(4):
        for k in range(4):
            for z in range(4):
                if((i != 3) and (j != 3) and (k != 3) and (z!=3)):
                    print(f"{arr[i]} {arr[j]} {arr[k]} {arr[z]}")

however it gives the following ..i.e the output can have one digit repeated but should contain atleast 1,5,8 …can any one suggest

1 1 1 1
1 1 1 5
1 1 1 8
1 1 5 1
1 1 5 5
1 1 5 8
1 1 8 1
1 1 8 5
1 1 8 8
1 5 1 1
1 5 1 5
1 5 1 8
1 5 5 1
1 5 5 5

finally i figured out the solution ..not sure why the post is closed and what is not clear about the question . the following is the code snippet that may help someone …if there is another shorter version it would be curious ..but for now here it goes

def GetCombFrom(arr):
for i in range(4):
    for j in range(4):
        for k in range(4):
            for z in range(4):
                if((i != 3) and (j != 3) and (k != 3) and (z!=3)):
                    if((i==j) and (j !=k) and (k!=z) and (i!=z) and (i!= k) and (j!=z)):
                        print(f"{arr[i]} {arr[j]} {arr[k]} {arr[z]}")
                    if((i==k) and (i !=j) and (j!=k) and (i!=z) and (k!=z) and (j!=z)):
                        print(f"{arr[i]} {arr[j]} {arr[k]} {arr[z]}")
                    if((i==z) and (i !=j) and (i!=k) and (j!=k) and (k!=z) and (j!=z)):
                       print(f"{arr[i]} {arr[j]} {arr[k]} {arr[z]}")
                    if((j==k) and (i !=j) and (i!=k) and (i!=z) and (k!=z) and (j!=z)):
                        print(f"{arr[i]} {arr[j]} {arr[k]} {arr[z]}")
                    if((k==z) and (i !=j) and (i!=k) and (i!=z) and (j!=k) and (j!=z)):
                        print(f"{arr[i]} {arr[j]} {arr[k]} {arr[z]}")
                    if((j==z) and (i !=j) and (i!=k) and (i!=z) and (j!=k) and (k!=z)):
                        print(f"{arr[i]} {arr[j]} {arr[k]} {arr[z]}")

output :

1 1 5 8
1 1 8 5
1 5 1 8
1 5 5 8
1 5 8 1
1 5 8 5
1 5 8 8
1 8 1 5
1 8 5 1
1 8 5 5
1 8 5 8
1 8 8 5
5 1 1 8
5 1 5 8
5 1 8 1
5 1 8 5
5 1 8 8
5 5 1 8
5 5 8 1
5 8 1 1
5 8 1 5
5 8 1 8
5 8 5 1
5 8 8 1
8 1 1 5
8 1 5 1
8 1 5 5
8 1 5 8
8 1 8 5
8 5 1 1
8 5 1 5
8 5 1 8
8 5 5 1
8 5 8 1
8 8 1 5
8 8 5 1
Asked By: Deepak N

||

Answers:

There is exactly 1 digit repeated. Iterate over the digit to chose that one. Place that digit into one of the 6 = 4 choose 2 pairs of places for that digit. There are 2 positions remaining to be filled by the other 2 digits. This can be done in 2 ways. Using itertools to select the 2 places for the repeated digits leads to the following code:

import itertools

def get_combos(digits):
    combos = []
    for d in digits:
        rest = [a for a in digits if a != d]
        combo = [0]*4
        for i,j in itertools.combinations(range(4),2):
            combo[i] = combo[j] = d
            k,l = [x for x in range(4) if x not in [i,j]]
            combo[k] = rest[0]
            combo[l] = rest[1]
            combos.append(combo.copy())
            combo[k] = rest[1]
            combo[l] = rest[0]
            combos.append(combo.copy())
    return combos

for combo in get_combos([1,5,8]):
    print(combo)

Output:

[1, 1, 5, 8]
[1, 1, 8, 5]
[1, 5, 1, 8]
[1, 8, 1, 5]
[1, 5, 8, 1]
[1, 8, 5, 1]
[5, 1, 1, 8]
[8, 1, 1, 5]
[5, 1, 8, 1]
[8, 1, 5, 1]
[5, 8, 1, 1]
[8, 5, 1, 1]
[5, 5, 1, 8]
[5, 5, 8, 1]
[5, 1, 5, 8]
[5, 8, 5, 1]
[5, 1, 8, 5]
[5, 8, 1, 5]
[1, 5, 5, 8]
[8, 5, 5, 1]
[1, 5, 8, 5]
[8, 5, 1, 5]
[1, 8, 5, 5]
[8, 1, 5, 5]
[8, 8, 1, 5]
[8, 8, 5, 1]
[8, 1, 8, 5]
[8, 5, 8, 1]
[8, 1, 5, 8]
[8, 5, 1, 8]
[1, 8, 8, 5]
[5, 8, 8, 1]
[1, 8, 5, 8]
[5, 8, 1, 8]
[1, 5, 8, 8]
[5, 1, 8, 8]
Answered By: John Coleman

This solution is somewhat fragile as it only considers an input of three distinct numbers to be put into a 4 element list. (It printed the 36 possibilties)

from sympy.utilities.iterables import multiset_permutations

L = [1,5,8]

for i in range(len(L)):
    j = L[:i] + [L[i]] * 2 +  L[i+1:]

    for perm in multiset_permutations(j):
        print(perm)

Output

[1, 1, 5, 8]
[1, 1, 8, 5]
[1, 5, 1, 8]
[1, 5, 8, 1]
[1, 8, 1, 5]
[1, 8, 5, 1]
[5, 1, 1, 8]
[5, 1, 8, 1]
[5, 8, 1, 1]
[8, 1, 1, 5]
[8, 1, 5, 1]
[8, 5, 1, 1]
[1, 5, 5, 8]
[1, 5, 8, 5]
[1, 8, 5, 5]
[5, 1, 5, 8]
[5, 1, 8, 5]
[5, 5, 1, 8]
[5, 5, 8, 1]
[5, 8, 1, 5]
[5, 8, 5, 1]
[8, 1, 5, 5]
[8, 5, 1, 5]
[8, 5, 5, 1]
[1, 5, 8, 8]
[1, 8, 5, 8]
[1, 8, 8, 5]
[5, 1, 8, 8]
[5, 8, 1, 8]
[5, 8, 8, 1]
[8, 1, 5, 8]
[8, 1, 8, 5]
[8, 5, 1, 8]
[8, 5, 8, 1]
[8, 8, 1, 5]
[8, 8, 5, 1]
Answered By: Chris Charley
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.