Skip to content

Recursion · stretch

Every subset

Write subsets(items) returning a list of every subset of the list (each subset a list, items in their original order). [1, 2] gives [], [1], [2], [1, 2] in any order.

Every subset either leaves out the first item or includes it — and the rest is the same problem, one item shorter.

Run adds print(subsets([1, 2, 3])) after your code, to try it.

Stuck on the idea rather than the code? The Recursion that halves, and recursion that branches lesson walks through it.