Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In python, Implement ’cartesianProduct’ and accompanying functions in the giving

ID: 3722623 • Letter: I

Question

In python, Implement ’cartesianProduct’ and accompanying functions in the giving code. The docstrings of each function elaborate on the constraints of each fn. Make sure to test your code.

Python code:

"""

Since elements of Python sets must be immutable,

a Python set cannot contain Python sets.

For example, a powerset cannot be represented using Python sets.

So we will simulate a set using lists.

For example, [[1,2], [1,3]] represents the set {{1,2}, {1,3}}.

This works, since lists do not have the immutability constraint.

"""

def randomSet (n, up):

"""Build a random 'set' of integers.

Be sure to guarantee the key property of a set: no duplicates.

>>> randomSet(4, 10)

[5, 3, 2, 8]

Params:

n (int): # of integers in the set

up (int): upperbound

Returns: (list) random 'set' of n integers between 0 and up, inclusive

"""

  

pass

  

def cartesianProduct (A, B):

"""Build the Cartesian product of the two sets A and B.

>>> cartesianProduct ([1,2], [3, 4])

[(1,3), (1,4), (2,3), (2,4)]

  

Params:

A (list): set of integers, represented internally as a list

B (list): set of integers, represented internally as a list

Returns: (list) Cartesian product AxB,

represented internally as a list of tuples

"""

pass

def printSet (L):

"""Print a 'set', using standard set notation.

To restore the correct interpretation, this function prints the 'set'

in true set notation.

Hint: you can print without adding a newline (see print documentation)

>>> printSet ([1,2,3,4])

{1, 2, 3, 4}

>>> printSet ([[1,2], [1,3]]

{{1,2}, {1,3}}

  

Params: A (list): set of integers

"""

pass

# an example of a test call

A = randomSet (5, 100)

B = randomSet (5, 100)

print ("The Cartesian product of ")

printSet(A)

print ("and")

printSet(B)

print ("is")

printSet (cartesianProduct (A, B))

Explanation / Answer

""" Since elements of Python sets must be immutable, a Python set cannot contain Python sets. For example, a powerset cannot be represented using Python sets. So we will simulate a set using lists. For example, [[1,2], [1,3]] represents the set {{1,2}, {1,3}}. This works, since lists do not have the immutability constraint. """ import random def randomSet(n, up): """Build a random 'set' of integers. Be sure to guarantee the key property of a set: no duplicates. [5, 3, 2, 8] Params: n (int): # of integers in the set up (int): upperbound Returns: (list) random 'set' of n integers between 0 and up, inclusive """ result = [] # Go through N times for i in range(n): # Append a random int between 0 to up to result randint = random.randint(0, up) if randint not in result: result.append(randint) # return result return result def cartesianProduct(A, B): """Build the Cartesian product of the two sets A and B. cartesianProduct ([1,2], [3, 4]) [(1,3), (1,4), (2,3), (2,4)] Params: A (list): set of integers, represented internally as a list B (list): set of integers, represented internally as a list Returns: (list) Cartesian product AxB, represented internally as a list of tuples """ result = [] for x in A: for y in B: result.append([x, y]) return result def printSingleSet(list): print "{", for i in range(len(list) - 1): print list[i], ",", print list[i], "}", def printSet(L): """Print a 'set', using standard set notation. To restore the correct interpretation, this function prints the 'set' in true set notation. Hint: you can print without adding a newline (see print documentation) printSet ([1,2,3,4]) {1, 2, 3, 4} printSet ([[1,2], [1,3]] {{1,2}, {1,3}} Params: A (list): set of integers """ if isinstance(L[0], int): printSingleSet(L) else: print "{", for x in L: print "{", x[0],",", x[1], "},", print "{" print A = randomSet(5, 100) B = randomSet(5, 100) print ("The Cartesian product of ") printSet(A) print ("and") printSet(B) print ("is") printSet(cartesianProduct(A, B))

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote