Embercode
✕
Recursion and BacktrackingQuick quiz

K-subsets

6 questions. Covers K-subsets in Python.

Questions 1 to 5 of 6

  1. 1. What does this program print? ``python def choose(items, k, start, current, result): if len(current) == k: result.append(current.copy()) return for i in range(start, len(items)): current.append(items[i]) choose(items, k, i + 1, current, result) current.pop() items = [1, 2, 3, 4] result = [] choose(items, 3, 0, [], result) print(result) print(f"count = {len(result)}") ``
  2. 2. What does this program print? ``python def choose(items, k, start, current, result): if len(current) == k: result.append(current.copy()) return for i in range(start, len(items)): if i > start and items[i] == items[i - 1]: continue current.append(items[i]) choose(items, k, i + 1, current, result) current.pop() items = [2, 1, 1, 3] items.sort() result = [] choose(items, 2, 0, [], result) print(result) print(f"count = {len(result)}") ``
  3. 3. How many groups of 3 can you make from 6 items, when the order inside a group does not matter? Write the number.

    Type your answer.

  4. 4. In the loop, after items[i] is chosen, the recursive call passes what new start index? Write it as an expression in i.

    Type your answer.

  5. 5. In the duplicate-skipping rule, why is the check i > start used and not i > 0?

5 question(s) left on this page.

Typed answers ignore extra spaces and, for most questions, capital letters.