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
- Hint 1Nudge
- Hint 2Approach Open hint 1 first
- Hint 3Pseudocode Open hint 2 first
- Worked solutionlast resort Opens after all 3 hints
Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.
| # | Test | Result | Reads · time |
|---|---|---|---|
| 1 | Three coaches | Not run | |
| 2 | None | Not run | |
| 3 | One | Not run | |
| 4 | Items keep their order | Not run | |
| 5 | Count | Not run | |
| 6 | Input not changed | Not run | |
| 7 | 16 coaches | Not run |