Question: Python3 Linked List Problem The Cartesian product of sets A and B is the set containing tuples (an ordered sequence of values) (x, y) where

Python3 Linked List Problem

Python3 Linked List Problem The Cartesian product of sets A and B

is the set containing tuples (an ordered sequence of values) (x, y)

The Cartesian product of sets A and B is the set containing tuples (an ordered sequence of values) (x, y) where x is an element of A and y is an element of B. For example, the Cartesian product of the sets {a, b} and {1, 2} is {(a, 1), (a, 2), (b, 1), (b, 2)}.

def cartesian_product(a, b): """ Returns the set containing tuples (x, y) where x is an element of a and y is an element of b.

>>> cartesian_product(Link(1, Link(2, Link(3))), Link.empty) is Link.empty True

>>> res = cartesian_product(Link('MATH'), Link('20C', Link('20D', Link(18)))) >>> len(res) 3 >>> set_contains(res, ('MATH', 18)) True >>> set_contains(res, ('MATH', '20C')) True >>> set_contains(res, ('MATH', '20D')) True

>>> res = cartesian_product(Link(True, Link(False)), Link(True, Link(False))) >>> len(res) 4 >>> set_contains(res, (True, True)) True >>> set_contains(res, (True, False)) True >>> set_contains(res, (False, True)) True >>> set_contains(res, (False, False)) True

>>> cartesian_product(Link.empty, Link.empty) is Link.empty 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)

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!