RECR-P3

Every group of coaches

Practice 3 of 6Recursion and backtracking7 tests

The problem

A train is being put together from the coaches in items. Return every possible group of coaches: every subset, from the empty one to all of them. Each subset is a list that keeps the coaches in the same order as items. The subsets themselves may be in any order.

subsets(["A", "B"])
# [["A", "B"], ["A"], ["B"], []]  (any order of the four)
subsets([])    # [[]]

itertools.combinations and itertools.product are switched off. The tests sort your answer before comparing it.

Limits

  • Up to 16 items, all different.

Builds on RECR-4 Subsets and permutations.

Continue on a laptop

Practice runs Python in the browser and needs a keyboard and a wider screen. Read the problem and open hints here, then write your solution on a laptop. Progress is saved per browser for now, so hints opened here stay on this device.

Need a hint?0 of 3 opened

  1. Hint 1Nudge
  2. Hint 2Approach Open hint 1 first
  3. Hint 3Pseudocode Open hint 2 first
  4. Worked solutionlast resort Opens after all 3 hints
Python waits for a wider screensolution.py

Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.

7 tests, not run yet
Test results
#TestResultReads · time
1Three coachesNot run
2NoneNot run
3OneNot run
4Items keep their orderNot run
5CountNot run
6Input not changedNot run
716 coachesNot run