Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to sort activities by their finish time.
DSA Python
activities.sort(key=lambda x: x[[1]])Attempts:
3 left
💡 Hint
Common Mistakes
Sorting by start time instead of finish time.
Using wrong index like 0 or 2.
✗ Incorrect
We sort activities by their finish time, which is at index 1 in each activity tuple.
2fill in blank
mediumComplete the code to select the first activity after sorting.
DSA Python
selected = [activities[[1]]]Attempts:
3 left
💡 Hint
Common Mistakes
Using index 1 which is the second activity.
Using negative or out of range indices.
✗ Incorrect
The first activity after sorting is at index 0.
3fill in blank
hardFix the error in the loop condition to check if the next activity can be selected.
DSA Python
for i in range(1, len(activities)): if activities[i][[1]] >= activities[last_selected][1]: selected.append(activities[i]) last_selected = i
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing finish time with finish time.
Using wrong indices like 1 or 2.
✗ Incorrect
We compare the start time (index 0) of the current activity with the finish time of the last selected activity.
4fill in blank
hardComplete the code to create a dictionary of selected activities with start time as key and finish time as value.
DSA Python
result = {: {BLANK_2}} for start, finish in selectedAttempts:
3 left
💡 Hint
Common Mistakes
Using square brackets instead of curly braces.
Swapping start and finish in keys and values.
✗ Incorrect
We create a dictionary comprehension with curly braces and use finish as the value.
5fill in blank
hardFill all three blanks to print the selected activities in order with format 'start -> finish'.
DSA Python
for start, finish in selected: print(str(start) + [1] + str(finish) + [2]) print([3])
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting spaces around arrow.
Printing newline inside loop causing extra blank lines.
✗ Incorrect
We join start and finish with ' -> ' and print without extra newline, then print a newline separately.
