Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started