Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.
Shade in the A or B circle for each question.
Draw a line connecting each item on the left to its match on the right.
| Left column | Right column | |
|---|---|---|
| 1if len(items) <= 1: return items | Asmaller = [x for x in items if x < pivot] | |
| 2return quick_sort(smaller) + equal + quick_sort(larger) | Bequal = [x for x in items if x == pivot] | |
| 3Build the smaller list by keeping only the items less than the pivot | CThe equal list is missing, so the pivot and any duplicates of it are dropped from the result. | |
| 4Keep duplicates of the pivot in an equal list so they are not lost or sorted again | DThe recursive step: sort the smaller and larger lists, then join them around the equal items. | |
| 5if len(items) == 5: return items | EThe base case tests for the wrong length. It should be len(items) <= 1, otherwise short lists never stop and the recursion goes on until it crashes. | |
| 6return quick_sort(smaller) + quick_sort(larger) | FThe base case: a list with 0 or 1 items is already sorted, so it is returned and the recursion stops. |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
return quick_sort(smaller) + equal + quick_sort(larger) — Recursive step of quick sort: sorts the smaller and larger lists by calling itself, then joins them around the equal items. Base case len(items) <= 1 stops it.if len(items) <= 1:
return itemssmaller = [x for x in items if x < pivot]larger = [x for x in items if x > pivot]equal = [x for x in items if x == pivot]return (quick_sort(smaller)
+ equal
+ quick_sort(larger))