My function combinations(ary, p) is suposed to return every possible combination, of order of items in list.
But it always returns the first value multiple times, even tho it finds all the possible orders, which I know because it prints them.
EDIT: I would like to make it work my self, learn something and not use any external libs.
def combinations(ary, p):
ln = len(ary)
bary=[]
for a in range(ln-p):
if p<ln-2:
bb = combinations(ary, p+1)
for be in bb:
bary.append(be)
if p>=ln-2:
bary.append(ary)
ary.append(ary.pop(p))
return bary
Another version with debug print() functions. I will give its sample output.
def combinations(ary, p):
ln = len(ary)
bary=[]
for a in range(ln-p):
if p<ln-2:
bb = combinations(ary, p+1)
for be in bb:
bary.append(be)
if p>=ln-2:
--> bary.append(ary)
--> print('ary', ary, 'bary', bary)
ary.append(ary.pop(p))
return bary
Console output after running with combinations([7,3,2], 0)::
## There is clearly every possible combination:
##
## ||
## ||
\/
ary [7, 3, 2] bary [[7, 3, 2]]
ary [7, 2, 3] bary [[7, 2, 3], [7, 2, 3]]
ary [3, 2, 7] bary [[3, 2, 7]]
ary [3, 7, 2] bary [[3, 7, 2], [3, 7, 2]]
ary [2, 7, 3] bary [[2, 7, 3]]
ary [2, 3, 7] bary [[2, 3, 7], [2, 3, 7]]
[[7, 3, 2], [7, 3, 2], [7, 3, 2], [7, 3, 2], [7, 3, 2], [7, 3, 2]]
The last list is suposed to include every possible order, but it has only the input value order even tho it prints every order. So where did I mess up the return?
print(list(itertools.permutations([7,3,2])))