Art of Computer Programming Volume 4: Fascicle 3 has a ton of these that might fit your particular situation better than how I describe.

## Gray Codes

An issue that you will come across is of course memory and pretty quickly, you'll have problems by 20 elements in your set -- ^{20}C_{3} = 1140. And if you want to iterate over the set it's best to use a modified gray code algorithm so you aren't holding all of them in memory. These generate the next combination from the previous and avoid repetitions. There are many of these for different uses. Do we want to maximize the differences between successive combinations? minimize? et cetera.

Some of the original papers describing gray codes:

- Some Hamilton Paths and a Minimal Change Algorithm
- Adjacent Interchange Combination Generation Algorithm

Here are some other papers covering the topic:

- An Efficient Implementation of the Eades, Hickey, Read Adjacent Interchange Combination Generation Algorithm (PDF, with code in Pascal)
- Combination Generators
- Survey of Combinatorial Gray Codes (PostScript)
- An Algorithm for Gray Codes

## Chase's Twiddle (algorithm)

Phillip J Chase, `Algorithm 382: Combinations of M out of N Objects' (1970)

The algorithm in C...

## Index of Combinations in Lexicographical Order (Buckles Algorithm 515)

You can also reference a combination by its index (in lexicographical order). Realizing that the index should be some amount of change from right to left based on the index we can construct something that should recover a combination.

So, we have a set {1,2,3,4,5,6}... and we want three elements. Let's say {1,2,3} we can say that the difference between the elements is one and in order and minimal. {1,2,4} has one change and is lexicographically number 2. So the number of 'changes' in the last place accounts for one change in the lexicographical ordering. The second place, with one change {1,3,4} has one change but accounts for more change since it's in the second place (proportional to the number of elements in the original set).

The method I've described is a deconstruction, as it seems, from set to the index, we need to do the reverse – which is much trickier. This is how Buckles solves the problem. I wrote some C to compute them, with minor changes – I used the index of the sets rather than a number range to represent the set, so we are always working from 0...n.
Note:

- Since combinations are unordered, {1,3,2} = {1,2,3} --we order them to be lexicographical.
- This method has an implicit 0 to start the set for the first difference.

## Index of Combinations in Lexicographical Order (McCaffrey)

There is another way:, its concept is easier to grasp and program but it's without the optimizations of Buckles. Fortunately, it also does not produce duplicate combinations:

The set that maximizes , where .

For an example: `27 = C(6,4) + C(5,3) + C(2,2) + C(1,1)`

. So, the 27th lexicographical combination of four things is: {1,2,5,6}, those are the indexes of whatever set you want to look at. Example below (OCaml), requires `choose`

function, left to reader:

```
(* this will find the [x] combination of a [set] list when taking [k] elements *)
let combination_maccaffery set k x =
(* maximize function -- maximize a that is aCb *)
(* return largest c where c < i and choose(c,i) <= z *)
let rec maximize a b x =
if (choose a b ) <= x then a else maximize (a-1) b x
in
let rec iterate n x i = match i with
| 0 -> []
| i ->
let max = maximize n i x in
max :: iterate n (x - (choose max i)) (i-1)
in
if x < 0 then failwith "errors" else
let idxs = iterate (List.length set) x k in
List.map (List.nth set) (List.sort (-) idxs)
```

## A small and simple combinations iterator

The following two algorithms are provided for didactic purposes. They implement an iterator and (a more general) folder overall combinations.
They are as fast as possible, having the complexity O(^{n}C_{k}). The memory consumption is bound by `k`

.

We will start with the iterator, which will call a user provided function for each combination

```
let iter_combs n k f =
let rec iter v s j =
if j = k then f v
else for i = s to n - 1 do iter (i::v) (i+1) (j+1) done in
iter [] 0 0
```

A more general version will call the user provided function along with the state variable, starting from the initial state. Since we need to pass the state between different states we won't use the for-loop, but instead, use recursion,

```
let fold_combs n k f x =
let rec loop i s c x =
if i < n then
loop (i+1) s c @@
let c = i::c and s = s + 1 and i = i + 1 in
if s < k then loop i s c x else f c x
else x in
loop 0 0 [] x
```

On Python ≥ 3.5, use `pathlib.Path.mkdir`

:

```
from pathlib import Path
Path("/my/directory").mkdir(parents=True, exist_ok=True)
```

For older versions of Python, I see two answers with good qualities, each with a small flaw, so I will give my take on it:

Try `os.path.exists`

, and consider `os.makedirs`

for the creation.

```
import os
if not os.path.exists(directory):
os.makedirs(directory)
```

As noted in comments and elsewhere, there's a race condition – if the directory is created between the `os.path.exists`

and the `os.makedirs`

calls, the `os.makedirs`

will fail with an `OSError`

. Unfortunately, blanket-catching `OSError`

and continuing is not foolproof, as it will ignore a failure to create the directory due to other factors, such as insufficient permissions, full disk, etc.

One option would be to trap the `OSError`

and examine the embedded error code (see Is there a cross-platform way of getting information from Python’s OSError):

```
import os, errno
try:
os.makedirs(directory)
except OSError as e:
if e.errno != errno.EEXIST:
raise
```

Alternatively, there could be a second `os.path.exists`

, but suppose another created the directory after the first check, then removed it before the second one – we could still be fooled.

Depending on the application, the danger of concurrent operations may be more or less than the danger posed by other factors such as file permissions. The developer would have to know more about the particular application being developed and its expected environment before choosing an implementation.

Modern versions of Python improve this code quite a bit, both by exposing `FileExistsError`

(in 3.3+)...

```
try:
os.makedirs("path/to/directory")
except FileExistsError:
# directory already exists
pass
```

...and by allowing a keyword argument to `os.makedirs`

called `exist_ok`

(in 3.2+).

```
os.makedirs("path/to/directory", exist_ok=True) # succeeds even if directory exists.
```

## Best Answer

This answer missed one aspect: the OP asked for ALL combinations... not just combinations of length "r".

So you'd either have to loop through all lengths "L":

Or -- if you want to get snazzy (or bend the brain of whoever reads your code after you) -- you can generate the chain of "combinations()" generators, and iterate through that: