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
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))
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