Question
Python3 Linked List Problem The symmetric difference of sets A and B is the set containing the elements of A and the elements of B
Python3 Linked List Problem
The symmetric difference of sets A and B is the set containing the elements of A and the elements of B that are not elements of both A and B. For example, the symmetric difference of the sets {a, b, c} and {c, d, e} is {a, b, d, e}.
def symmetric_diff(a, b): """ Returns the elements in the set a or the set b, but not both.
>>> res = symmetric_diff(Link('a', Link('b')), Link(1, Link(2))) >>> len(res) 4 >>> set_contains(res, 'a') True >>> set_contains(res, 'b') True >>> set_contains(res, 1) True >>> set_contains(res, 2) True
>>> res = symmetric_diff(Link('a', Link('b', Link('c'))), Link(1, Link(2, Link('c')))) >>> len(res) 4 >>> set_contains(res, 'a') True >>> set_contains(res, 'b') True >>> set_contains(res, 1) True >>> set_contains(res, 2) True
>>> symmetric_diff(Link('a', Link('b', Link('c'))), Link('a', Link('b', Link('c')))) is Link.empty True
>>> res = symmetric_diff(Link(1, Link(2, Link(3))), Link.empty) >>> len(res) 3 >>> set_contains(res, 1) True >>> set_contains(res, 2) True >>> set_contains(res, 3) True """ """YOUR CODE GOES HERE"""
class Link: A Linked list with a first element and the rest." empty( def _init_(self, first, rest-empty): assert rest is Link.empty or isinstance(rest, Link) self.first first self.rest rest def_getitem(self, i): return self.first else: return self.rest [i-1] def len_(self) return 1 +len(self.rest) def-repr--(self): if self.rest is Link.empty: return 'Link(+ repr(self.first)+ return Link(' + repr(self.first) + + repr(self.rest) + ') def set_contains (s, v): "Return True if and only if set s contains v." if s is Link.empty: return False elif s.first V: return True else: return set_contains (s.rest, v) class Link: A Linked list with a first element and the rest." empty( def _init_(self, first, rest-empty): assert rest is Link.empty or isinstance(rest, Link) self.first first self.rest rest def_getitem(self, i): return self.first else: return self.rest [i-1] def len_(self) return 1 +len(self.rest) def-repr--(self): if self.rest is Link.empty: return 'Link(+ repr(self.first)+ return Link(' + repr(self.first) + + repr(self.rest) + ') def set_contains (s, v): "Return True if and only if set s contains v." if s is Link.empty: return False elif s.first V: return True else: return set_contains (s.rest, v)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