Complete the code to identify unreachable objects in a heap.
unreachable_objects = [obj for obj in heap if obj not in [1]]
The roots set contains all objects directly accessible by the program. Objects not reachable from roots are considered unreachable.
Complete the code to mark reachable objects during garbage collection.
def mark(obj): if obj not in marked: marked.add(obj) for ref in obj.[1]: mark(ref)
Each object has references to other objects. We recursively mark all referenced objects as reachable.
Fix the error in the code to sweep unmarked objects from the heap.
for obj in heap[:]: if obj not in [1]: heap.remove(obj)
Only objects not marked as reachable should be removed from the heap during the sweep phase.
Fill both blanks to implement reference counting update after object assignment.
def assign(obj, new_ref): obj.ref_count [1] 1 new_ref.ref_count [2] 1
When an object reference is replaced, the old object's reference count decreases and the new object's reference count increases.
Fill both blanks to create a dictionary comprehension that maps objects to their reference counts if count is greater than zero.
ref_counts = {obj[1]: obj.ref_count : obj.ref_count for obj in heap if obj.ref_count [2] 0}The dictionary maps object IDs to their reference counts, but only includes objects with positive reference counts.