Question: # You are not allowed to import any modules whatsoever # Assume a set is represented as a Python list # Do not modify these


# You are not allowed to import any modules whatsoever
# Assume a set is represented as a Python list
# Do not modify these
A = [1, 2, 5, 8, 12, 5]
B = [3, 5, 7, 8, 11, 3]
# Define a function that takes in a set, represented as a Python list,
# and returns an equivalent set with all duplicates removed
def simplify(A):
# Provide your code here...
# Testing the simplify function
print "A = ", simplify(A)
print "B = ", simplify(B)
# Expected:
# A = [1, 2, 5, 8, 12]
# B = [3, 5, 7, 8, 11]
# Define a function that takes in two sets and returns their union.
# The resulting set should be a Python list with no duplicates
def union(A, B):
# Provide your code here...
# Testing the union function
print "A union B = ", union(A, B)
# Expected:
# A union B = [1, 2, 5, 8, 12, 3, 7, 11]
# Define a function that takes in two sets and returns their intersection.
# The resulting set should be a Python list with no duplicates
def intersection(A, B):
# Provide your code here...
# Testing the intersection function
print "A intersection B = ", intersection(A, B)
# Expected:
# A intersection B = [5, 8]
# Define a function that takes in two sets, A and B, and returns True
# if A is a subset of B, and False otherwise
def subset(A, B):
# Provide your code here...
# Testing the subset function
print "A subset of B:", subset(A, B)
print "[5, 7, 8] subset of B:", subset([5,7,8], B)
# Expected:
# A subset of B: False
# [5, 7, 8] subset of B: True
# Define a function that takes in two sets and returns True
# if they are equal, and False otherwise
def equal(A, B):
# Provide your code here...
# Testing the equal function
print "A == B:", equal(A, B)
print "[1, 2, 3, 4] == [1, 1, 2, 2, 4, 2, 3, 3]:", equal([1, 2, 3, 4],[1,
1, 2, 2, 4, 2, 3, 3])
# Expected:
# A == B: False
# [1, 2, 3, 4] == [1, 1, 2, 2, 4, 2, 3, 3]: True
# Define a function that takes in two sets and returns their Cartesian
product
# The result should be represented as a list of tuples, ex: [(1, 1), (1,
2), ...]
def cartesian_product(A, B):
# Provide your code here...
# Testing the cartesian_product function
print "[1] x B =", cartesian_product([1], B)
print "[1, 2] x B =", cartesian_product([1, 2], B)
Checking a Condition on List Elements Many of the exercises in this lab will involve checking whether a certain condition is satisfied for every list element. There is a useful construct, sometimes referred to as a flag, which keeps track of the condition as we are going through the loop. To make use of it, we have been given a list, and we assume the condition is true for every element. We set up the code like this: theList-[...1 # Let's assume the condition we are testing is true flag - True for i in theList: if condition does not hold for i # We found an element that does not meet the condition, we are done flag False break # When we are done with the for-loop, the flag will either be True of False # corresponding to whether the condition is true for all elements of the list. # Since we started with flag true, and the only way to make it false is to # find a list element for which the condition is not true, this means that if # flag is false at this point of the code, we must have found a list element that # does not meet the condition, so the condition can't hold for every element Building up a List Sometimes the solution to a problem is to go through a given list (possibly more than one list), and select only those elements for which a certain condition holds, and make that the result. We set up the code in the following way: inputList = [ result- [] # At the start result is empty for i in inputList ..] if condition holds for i: esult.append(i) # At this point result is made up of only those input elements that met the condition
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
