Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start backtracking by calling the helper function.
DSA Python
def backtrack(path, choices): if not choices: print(path) return # Start backtracking with empty path and full choices backtrack([1], [1, 2, 3])
Attempts:
3 left
💡 Hint
Common Mistakes
Starting with None instead of an empty list.
Starting with a list containing elements instead of empty.
✗ Incorrect
We start backtracking with an empty path represented by an empty list [].
2fill in blank
mediumComplete the code to choose the next option in backtracking.
DSA Python
def backtrack(path, choices): if not choices: print(path) return for i in range(len(choices)): backtrack(path + [choices[[1]]], choices[:i] + choices[i+1:])
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 always, which picks only the first choice repeatedly.
Using len(choices), which is out of range.
✗ Incorrect
We use index i to pick the current choice from the list.
3fill in blank
hardFix the error in the backtracking base case to print the complete path.
DSA Python
def backtrack(path, choices): if len(choices) == 0: print([1]) return
Attempts:
3 left
💡 Hint
Common Mistakes
Printing choices instead of path.
Printing None or length instead of the actual path.
✗ Incorrect
We print the current path when no choices remain to show the complete solution.
4fill in blank
hardFill both blanks to create a dictionary comprehension that maps each choice to its square if the choice is even.
DSA Python
result = { [1] : [2] for x in [1, 2, 3, 4] if x % 2 == 0 }Attempts:
3 left
💡 Hint
Common Mistakes
Using x*2 instead of x**2 for the value.
Using x+1 which does not square the number.
✗ Incorrect
We map each even number x to its square x**2 in the dictionary.
5fill in blank
hardFill all three blanks to create a dictionary comprehension that maps uppercase keys to values greater than 10.
DSA Python
filtered = { [1] : [2] for k, v in data.items() if v [3] 10 }Attempts:
3 left
💡 Hint
Common Mistakes
Using k instead of k.upper() for keys.
Using <= instead of > for filtering values.
✗ Incorrect
Keys are converted to uppercase with k.upper(), values are v, and we filter values greater than 10 with >.
