Question

Write a Python function cardinality() that takes in three Python set objects, representing sets of between...

Write a Python function cardinality() that takes in three Python set objects, representing sets of between 0 and 50 integers, AA, BB, and UU. Your function should return a single non-negative integer value for the cardinality of the set below. AA and BB are subsets (not necessarily proper) of the universal set UU.

|P(A¯¯¯¯∩B)||P(A¯∩B)|


Note 1: You can copy-paste the code declaring the various visible test cases below. We strongly encourage you to do this to test your code.

Note 2: Your function should take in the sets in the order: AA, BB, UU.

For example:

Test Result
A = {0, 1, 2}
B = {1, 3}
U = {0, 1, 2, 3, 4}
print(cardinality(A, B, U))
2
A = {0, 2, 5, 10, 11, 12, 15, 16, 19, 23, 24, 30}
B = {1, 3, 4, 6, 7, 15, 16, 22, 23, 25, 29}
U = set(list(range(0,31)))
print(cardinality(A, B, U))
256
0 0
Add a comment Improve this question Transcribed image text
Answer #1
def cardinality(A, B, U):
    return 2 ** len(U.difference(A).intersection(B))


# Below code is used to test the code. ignore/remove the code below
A = {0, 1, 2}
B = {1, 3}
U = {0, 1, 2, 3, 4}
print(cardinality(A, B, U))

A = {0, 2, 5, 10, 11, 12, 15, 16, 19, 23, 24, 30}
B = {1, 3, 4, 6, 7, 15, 16, 22, 23, 25, 29}
U = set(list(range(0, 31)))
print(cardinality(A, B, U))

Add a comment
Know the answer?
Add Answer to:
Write a Python function cardinality() that takes in three Python set objects, representing sets of between...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Python function powerSet() that takes in a finite list object AA and returns P(A)P(A),...

    Write a Python function powerSet() that takes in a finite list object AA and returns P(A)P(A), the power set of A. The output should be only in the form of list objects. Note: Make sure you are familiar with some of Python's basic built-in list functions and operators, as they will make completing this problem far easier. For example: Test Result A = [0, 1, 2] output = set([frozenset(a) for a in powerSet(A)]) expected = [[], [0], [0, 1], [2],...

  • Sets A, B, and Care subsets of the universal set U. These sets are defined as...

    Sets A, B, and Care subsets of the universal set U. These sets are defined as follows. U= {1, 2, 3, 4, 5, 6, 7, 8, 9} A = {1,6,7,8,9} B = {1, 3, 4, 7, 9) C = {4, 5, 6, 7} Find CU ( BA)'. Write your answer in roster form or as Ø. CU (BNA): = 0 Х 5 ?

  • Let S be the universal set, where: S = {1, 2, 3, ..., 18, 19, 20}...

    Let S be the universal set, where: S = {1, 2, 3, ..., 18, 19, 20} Let sets A and B be subsets of S, where: Set A = {2,5, 6, 7, 8, 14, 18} Set B = {1, 2, 3, 4, 7, 9, 10, 11, 12, 14, 18, 19, 20} Find the following: The cardinality of the set (A U B): n(AUB) = The cardinality of the set (A n B): n(An B) is You may want to draw...

  • Write a function in python that takes a set A and a relation R(x, y) on...

    Write a function in python that takes a set A and a relation R(x, y) on A (as a python function such that R(x, y) returns true if and only if the relation xRy holds), and returns True if and only if the relation R is reflexive. Here is the function signature you need to use. def is reflexive(A, R): You can test your code as follows. s = {1,2,3} def y(x, y): return x == y def n(x, y):...

  • Python 1. Open up IDLE and in a multiline comment write “Functions and Sets II,” your...

    Python 1. Open up IDLE and in a multiline comment write “Functions and Sets II,” your name, and the date. 2. Use Python to write a function f(n) that finds the sum of the first n positive integers. Do not use the sum() function or lists. Write the function from scratch. Then use a for loop to print the sums for n = 10, 50, 1000. 3. Finding the Image of a function Suppose that we have a function f...

  • python Write a function that takes as input a single integer parametern and computes the nth...

    python Write a function that takes as input a single integer parametern and computes the nth Fibonacci Sequence number The Fibonacci sequence first two terms are 1 and 1(so, if n - 2 your function should return 1). From there, the previous two values are summed to come up with the next result in the sequence 1.1,2,3,5,8,13, 21, 34, 55, etc.) For example, if the input is 7 then your function should return 13 26561.1615880 1 def Fibonacci(n): # your...

  • USE PYTHON PLEASE Write a function called is prime which takes a single integer argument and...

    USE PYTHON PLEASE Write a function called is prime which takes a single integer argument and returns a single Boolean value representing whether the given argument is prime (True) or not (False). After writing the function, test it by using a loop to print out all the prime numbers from 1-100. To check your results, the prime numbers from 1-100 are: 2, 3, 5, 7, 11. 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67,...

  • Please write a python function for : greedy: This function takes two inputs: one a graph,...

    Please write a python function for : greedy: This function takes two inputs: one a graph, and the other an ordering of the vertices as a list, and returns the proper vertex-coloring produced by the greedy algorithm over the ordering in the list. Examples: greedy({"A" : ["B", "C"], "B" : ["A"], "C" : ["A"]},[“A”, “B”, “C”]) should return {“A” : 1, “B” : 2, "C" : 2} greedy({“A” : ["B"], "B" : [“A”, “C”],"C" : [“B”, “D”), “D" : ["C"]},[“A”,...

  • Using python, Write a function max3 that takes three numbers as its arguments and returns the...

    Using python, Write a function max3 that takes three numbers as its arguments and returns the greatest of these three numbers. Your submitted file for this problem should include (a) function definition, obviously, and (b) the code that calls that function (for this problem you do not have to package that calling code into the main() function although you may if you want to), so that if I run your *.py file it computes and prints something (e.g. few test...

  • python . Write the function poker_hand that takes a list of exactly five distinct Card objects...

    python . Write the function poker_hand that takes a list of exactly five distinct Card objects as an argument, analyzes the list, and returns one of the following strings that describes the hand: "Four of a kind' (four cards of the same rank) 'Full house' (three cards of one rank, and two cards of a different rank) . 'Flush' (five cards of the same suit) 'Three of a kind' (exactly three cards of the same rank) • 'One pair' (at...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT