Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

Below is a "standard recursive answer", similar to the other similar answer http://stackoverflow.com/a/23743696/711085https://stackoverflow.com/a/23743696/711085 . (We don't realistically have to worry about running out of stack space since there's no way we could process all N! permutations.)

Below is a "standard recursive answer", similar to the other similar answer http://stackoverflow.com/a/23743696/711085 . (We don't realistically have to worry about running out of stack space since there's no way we could process all N! permutations.)

Below is a "standard recursive answer", similar to the other similar answer https://stackoverflow.com/a/23743696/711085 . (We don't realistically have to worry about running out of stack space since there's no way we could process all N! permutations.)

added 132 characters in body
Source Link
ninjagecko
  • 91.6k
  • 24
  • 144
  • 153

It visits every element in turn, and either takes it or leaves it (we can directly see the 2^N cardinality from this algorithm).

It visits every element in turn, and either takes it or leaves it (we can directly see the 2^N cardinality from this algorithm).

Post Undeleted by ninjagecko
added 145 characters in body
Source Link
ninjagecko
  • 91.6k
  • 24
  • 144
  • 153

Below is a "standard recursive answer", similar to the other similar answer http://stackoverflow.com/a/23743696/711085 . We(We don't realistically have to worry about running out of stack space since there's no way we could process all N! permutations, though if you were making the "get k-rank permutation / successor permutation" function, it would avoid this problem.)

def permscombs(xs, i=0):
    if xs==[]i==len(xs):
        yield []()
        return
    for i,xc in enumeratecombs(xs,i+1):
        subset =yield list(xs)c
        subset.popyield c+(ixs[i],)

Demo:

>>> list( combs(range(5)) )
[(), (0,), (1,), (1, 0), for(2,), p(2, in0), perms(subset2, 1):
, (2, 1, 0), (3,), (3, 0), (3, 1), (3, 1, 0), yield(3, p+[x]2), (3, #2, could0), reverse(3, order2, may1), be(3, slightly2, less1, efficient

Demo:

>>>0), list(perms4,), ([14,2 0),3 (4,4] 1), (4, 1, 0)
[[4, 3(4, 2), 1](4, [32, 0), (4, 2, 1]1), [4(4, 2, 31, 1]0), [2(4, 3), (4, 3, 1]0), 
 (4, [33, 21), (4, 1]3, [21, 0), (4, 3, 2), (4, 1]3, [42, 0), (4, 3, 2, 1), 2](4, [33, 42, 1, 2]0)]

>>> list(sorted( combs(range(5)), key=len))
[(), [4
 (0,), (1,), (2,), (3,), 2](4,), [1
 (1, 40), (2, 0), (2, 1), (3, 2]0), [3(3, 1), (3, 2), (4, 2]0), [1(4, 31), (4, 2]2), 
 (4, [43), 
 (2, 1, 3]0), [2(3, 41, 10), 3](3, [42, 10), (3, 2, 3]1), [1(4, 1, 0), (4, 2, 3]0), 
 (4, [22, 1), (4, 3]3, [10), 2(4, 3, 1), (4, 3]3, [32), 
 (3, 2, 1, 4]0), [2(4, 32, 1, 4]0), 
 [3(4, 13, 21, 4]0), [1(4, 3, 2, 4]0), [2(4, 13, 32, 4]1), [1
 (4, 3, 2, 31, 4]]0)]

>>> len(set(tuplecombs(x) for x in permsrange([1,2,3,4]5))))
2432

Below is a "standard recursive answer", similar to the other similar answer http://stackoverflow.com/a/23743696/711085 . We don't realistically have to worry about running out of stack space since there's no way we could process all N! permutations, though if you were making the "get k-rank permutation / successor permutation" function, it would avoid this problem.

def perms(xs):
    if xs==[]:
        yield []
    for i,x in enumerate(xs):
        subset = list(xs)
        subset.pop(i)
        for p in perms(subset):
            yield p+[x]  # could reverse order, may be slightly less efficient

Demo:

>>> list(perms([1,2,3,4]))
[[4, 3, 2, 1], [3, 4, 2, 1], [4, 2, 3, 1], [2, 4, 3, 1], 
  [3, 2, 4, 1], [2, 3, 4, 1], [4, 3, 1, 2], [3, 4, 1, 2], 
 [4, 1, 3, 2], [1, 4, 3, 2], [3, 1, 4, 2], [1, 3, 4, 2], 
  [4, 2, 1, 3], [2, 4, 1, 3], [4, 1, 2, 3], [1, 4, 2, 3], 
  [2, 1, 4, 3], [1, 2, 4, 3], [3, 2, 1, 4], [2, 3, 1, 4], 
 [3, 1, 2, 4], [1, 3, 2, 4], [2, 1, 3, 4], [1, 2, 3, 4]]

>>> len(set(tuple(x) for x in perms([1,2,3,4])))
24

Below is a "standard recursive answer", similar to the other similar answer http://stackoverflow.com/a/23743696/711085 . (We don't realistically have to worry about running out of stack space since there's no way we could process all N! permutations.)

def combs(xs, i=0):
    if i==len(xs):
        yield ()
        return
    for c in combs(xs,i+1):
        yield c
        yield c+(xs[i],)

Demo:

>>> list( combs(range(5)) )
[(), (0,), (1,), (1, 0), (2,), (2, 0), (2, 1), (2, 1, 0), (3,), (3, 0), (3, 1), (3, 1, 0), (3, 2), (3, 2, 0), (3, 2, 1), (3, 2, 1, 0), (4,), (4, 0), (4, 1), (4, 1, 0), (4, 2), (4, 2, 0), (4, 2, 1), (4, 2, 1, 0), (4, 3), (4, 3, 0), (4, 3, 1), (4, 3, 1, 0), (4, 3, 2), (4, 3, 2, 0), (4, 3, 2, 1), (4, 3, 2, 1, 0)]

>>> list(sorted( combs(range(5)), key=len))
[(), 
 (0,), (1,), (2,), (3,), (4,), 
 (1, 0), (2, 0), (2, 1), (3, 0), (3, 1), (3, 2), (4, 0), (4, 1), (4, 2), (4, 3), 
 (2, 1, 0), (3, 1, 0), (3, 2, 0), (3, 2, 1), (4, 1, 0), (4, 2, 0), (4, 2, 1), (4, 3, 0), (4, 3, 1), (4, 3, 2), 
 (3, 2, 1, 0), (4, 2, 1, 0), (4, 3, 1, 0), (4, 3, 2, 0), (4, 3, 2, 1), 
 (4, 3, 2, 1, 0)]

>>> len(set(combs(range(5))))
32
Post Deleted by ninjagecko
Source Link
ninjagecko
  • 91.6k
  • 24
  • 144
  • 153
Loading