Python Forum
HELP ME - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: HELP ME (/thread-31099.html)



HELP ME - sabe - Nov-23-2020

I wrote the code, but each digit with 3 digits could not reflect a different 4 permutation please help.

def comb(L): 
      
    for i in range(3): 
        for j in range(3): 
            for k in range(3): 
                  
              
                if (i!=j and j!=k and i!=k): 
                    print(L[i], L[j], L[k]) 
                      

comb([1, 2, 3, 4])



RE: HELP ME - Superjoe - Nov-23-2020

try:
def comb(l):
    size = len(l)
    for i in range(size):
        for j in range(size):
            for k in range(size):
                if i != j and j != k and i != k:
                    print(l[i], l[j], l[k])


comb([1, 2, 3, 4])



RE: HELP ME - deanhystad - Nov-23-2020

I would expect a function named "comb" to generate combination. Superjoe's code, and the code it looks like you are trying to write, make some kind of weird permutation where the values for each entry must be unique. Is that what you were trying to do?

If you need combinations or permutations for an assignment I suggest using the functions in the itertools standard library. Unless, of course, your assignment is to write a combinations function.