8 questions. Covers Quickselect in Python.
Questions 1 to 5 of 8
Use the arrows to put the steps in order.
python def merge(intervals): if not intervals: return [] ordered = sorted(intervals, key=lambda iv: iv[0]) merged = [] current = list(ordered[0]) for start, end in ordered[1:]: if start <= current[1]: current[1] = max(current[1], end) else: merged.append(current) current = [start, end] merged.append(current) return merged print(merge([[6, 7], [1, 2], [2, 4], [4, 5]])) print(merge([[3, 9], [1, 2], [5, 6]]))
python def partition(a, lo, hi): pivot = a[hi] i = lo - 1 for j in range(lo, hi): if a[j] < pivot: i += 1 a[i], a[j] = a[j], a[i] a[i + 1], a[hi] = a[hi], a[i + 1] return i + 1 def quick_select(a, k): lo = 0 hi = len(a) - 1 while lo < hi: p = partition(a, lo, hi) print(f'pivot {a[p]} ends at {p}, array {a}') if p == k: return a[p] if k < p: hi = p - 1 else: lo = p + 1 return a[lo] print(f'result: {quick_select([5, 3, 8, 1, 9, 2], 3)}')
python current = list(ordered[0]) for start, end in ordered[1:]: if start <= current[1]: current[1] = end else: merged.append(current) current = [start, end] merged.append(current)
3 question(s) left on this page.