Question: Define intersect, which returns a set containing only values that appear in both sets s and t. Your implementation should run in linear time in

Define intersect, which returns a set containing only values that appear in both sets s and t. Your implementation should run in linear time in the length of the input sets. A Python implementation of this procedure is provided for your reference.

Also, define union, which returns a set containing all values that appear in either set s or t.

(define (intersect s t) (cond ((or (empty? s) (empty? t)) nil) 'YOUR-CODE-HERE (else nil) ; replace this line )) ; Equivalent Python code, for your reference: ; ; def intersect(set1, set2): ; if empty(set1) or empty(set2): ; return Link.empty ; else: ; e1, e2 = set1.first, set2.first ; if e1 == e2: ; return Link(e1, intersect(set1.rest, set2.rest)) ; elif e1 < e2: ; return intersect(set1.rest, set2) ; elif e2 < e1: ; return intersect(set1, set2.rest) (define (union s t) (cond ((empty? s) t) ((empty? t) s) 'YOUR-CODE-HERE (else nil) ; replace this line ))

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!