Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print the move from source to destination.
DSA Python
def move_disk([1], source, destination): print(f"Move disk [1] from {source} to {destination}")
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names that do not represent the disk number.
Confusing source and destination parameters.
✗ Incorrect
The function parameter should be 'n' to represent the disk number being moved.
2fill in blank
mediumComplete the code to call the tower_of_hanoi function recursively for n-1 disks.
DSA Python
def tower_of_hanoi([1], source, auxiliary, destination): if n == 1: move_disk(n, source, destination) return tower_of_hanoi(n-1, source, destination, auxiliary) move_disk(n, source, destination) tower_of_hanoi(n-1, auxiliary, source, destination)
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different variable name than in recursive calls.
Not matching the parameter with the recursive calls.
✗ Incorrect
The function parameter should be 'n' to represent the number of disks.
3fill in blank
hardFix the error in the base case condition to stop recursion correctly.
DSA Python
def tower_of_hanoi(n, source, auxiliary, destination): if n [1] 1: move_disk(n, source, destination) return tower_of_hanoi(n-1, source, destination, auxiliary) move_disk(n, source, destination) tower_of_hanoi(n-1, auxiliary, source, destination)
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' or '<' which causes incorrect recursion.
Using '!=' which never stops recursion properly.
✗ Incorrect
The base case should check if n is equal to 1 to stop recursion.
4fill in blank
hardFill both blanks to correctly call the recursive steps in Tower of Hanoi.
DSA Python
def tower_of_hanoi(n, source, auxiliary, destination): if n == 1: move_disk(n, source, destination) return tower_of_hanoi(n-1, source, [1], [2]) move_disk(n, source, destination) tower_of_hanoi(n-1, auxiliary, source, destination)
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping source and auxiliary incorrectly.
Using 'n' as a rod name.
✗ Incorrect
The first recursive call moves n-1 disks from source to auxiliary using destination as temporary.
5fill in blank
hardFill all three blanks to create a dictionary comprehension that maps disk numbers to their move count if greater than zero.
DSA Python
moves = [1]: [2] for [3] in range(1, n+1) if [2] > 0
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same variable name for key and loop variable incorrectly.
Not matching the variable names in the comprehension.
✗ Incorrect
The comprehension maps each disk to its count if count is greater than zero.
